summaryrefslogtreecommitdiff
path: root/pb_migrations/1730000000_bootstrap.js
blob: 3a4bfcaa10f421573e3065791db544d8d251e3eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
migrate((app) => {
    let superusers = app.findCollectionByNameOrId("_superusers")

    let superuser = new Record(superusers)
    superuser.set("email", "admin@example.com")
    superuser.set("password", "change-me-now")
    app.save(superuser)

    let users = new Collection({
        type: "auth",
        name: "proxy_users",
        listRule: null,
        viewRule: null,
        createRule: "",
        updateRule: "@request.auth.id = id",
        deleteRule: null,
        fields: [
            {
                name: "username",
                type: "text",
                required: true,
                presentable: true,
                min: 3,
                max: 64,
                pattern: "^[a-zA-Z0-9_.-]+$",
            },
            {
                name: "role",
                type: "select",
                required: true,
                maxSelect: 1,
                values: ["user", "admin"],
            },
            {
                name: "is_active",
                type: "bool",
            },
            {
                name: "failed_logins",
                type: "number",
                onlyInt: true,
                min: 0,
            },
            {
                name: "last_login_at",
                type: "date",
            },
        ],
        indexes: [
            "CREATE UNIQUE INDEX idx_proxy_users_username ON proxy_users (username)",
        ],
        passwordAuth: {
            enabled: true,
            identityFields: ["username", "email"],
        },
    })
    app.save(users)

    let usersCollection = app.findCollectionByNameOrId("proxy_users")
    let admin = new Record(usersCollection)
    admin.set("username", "admin")
    admin.set("email", "proxy-admin@example.com")
    admin.set("password", "password123")
    admin.set("passwordConfirm", "password123")
    admin.set("verified", true)
    admin.set("emailVisibility", false)
    admin.set("role", "admin")
    admin.set("is_active", true)
    admin.set("failed_logins", 0)
    app.save(admin)
}, (app) => {
    try {
        let adminRecord = app.findAuthRecordByEmail("proxy_users", "proxy-admin@example.com")
        app.delete(adminRecord)
    } catch (_) {}

    try {
        let usersCollection = app.findCollectionByNameOrId("proxy_users")
        app.delete(usersCollection)
    } catch (_) {}

    try {
        let superuser = app.findAuthRecordByEmail("_superusers", "admin@example.com")
        app.delete(superuser)
    } catch (_) {}
})