| 18 | * @returns {Promise} |
| 19 | */ |
| 20 | const setupDefaultUser = async () => { |
| 21 | const initialAdminEmail = process.env.INITIAL_ADMIN_EMAIL; |
| 22 | const initialAdminPassword = process.env.INITIAL_ADMIN_PASSWORD; |
| 23 | |
| 24 | // This will only create a new user when there are no active users in the database |
| 25 | // and the INITIAL_ADMIN_EMAIL and INITIAL_ADMIN_PASSWORD environment variables are set. |
| 26 | // Otherwise, users should be shown the setup wizard in the frontend. |
| 27 | // I'm keeping this legacy behavior in case some people are automating deployments. |
| 28 | |
| 29 | if (!initialAdminEmail || !initialAdminPassword) { |
| 30 | return Promise.resolve(); |
| 31 | } |
| 32 | |
| 33 | const userIsetup = await isSetup(); |
| 34 | if (!userIsetup) { |
| 35 | // Create a new user and set password |
| 36 | logger.info(`Creating a new user: ${initialAdminEmail} with password: ${initialAdminPassword}`); |
| 37 | |
| 38 | const data = { |
| 39 | is_deleted: 0, |
| 40 | email: initialAdminEmail, |
| 41 | name: "Administrator", |
| 42 | nickname: "Admin", |
| 43 | avatar: "", |
| 44 | roles: ["admin"], |
| 45 | }; |
| 46 | |
| 47 | const user = await userModel |
| 48 | .query() |
| 49 | .insertAndFetch(data); |
| 50 | |
| 51 | await authModel |
| 52 | .query() |
| 53 | .insert({ |
| 54 | user_id: user.id, |
| 55 | type: "password", |
| 56 | secret: initialAdminPassword, |
| 57 | meta: {}, |
| 58 | }); |
| 59 | |
| 60 | await userPermissionModel.query().insert({ |
| 61 | user_id: user.id, |
| 62 | visibility: "all", |
| 63 | proxy_hosts: "manage", |
| 64 | redirection_hosts: "manage", |
| 65 | dead_hosts: "manage", |
| 66 | streams: "manage", |
| 67 | access_lists: "manage", |
| 68 | certificates: "manage", |
| 69 | }); |
| 70 | logger.info("Initial admin setup completed"); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * Creates default settings if they don't already exist in the database |