Create first admin user

This commit is contained in:
Ben Allfree 2023-02-20 08:45:59 -08:00
parent 28029af0b1
commit 64b21bd2db
4 changed files with 22 additions and 6 deletions

View File

@ -3,8 +3,8 @@ PUBLIC_APP_DOMAIN=pockethost.test
PUBLIC_APP_DB=pockethost-central
DAEMON_PB_BIN_DIR=`pwd`/packages/pocketbase/dist
DAEMON_PB_DATA_DIR=`pwd`/.data
DAEMON_PB_USERNAME=#ADDME
DAEMON_PB_PASSWORD=#FIXME
DAEMON_PB_USERNAME=admin@pockethost.test
DAEMON_PB_PASSWORD=password
DAEMON_PB_PORT=8090
DAEMON_IDLE_TTL=5000
DAEMON_PB_BACKUP_SLEEP=100

View File

@ -32,6 +32,7 @@ export const withInstance = safeCatch(
`***WARNING*** CANNOT AUTHENTICATE TO ${PUBLIC_APP_PROTOCOL}://${PUBLIC_APP_DB}.${PUBLIC_APP_DOMAIN}/_/`
)
error(`***WARNING*** LOG IN MANUALLY, ADJUST .env, AND RESTART DOCKER`)
process.exit(-1)
} finally {
info(`Exiting process`)
mainProcess.kill()

View File

@ -33,6 +33,11 @@ export const createPbClient = (url: string) => {
client.admins.authWithPassword(email, password)
)
const createFirstAdmin = safeCatch(
`createFirstAdmin`,
(email: string, password: string) => client.admins.create(email, password)
)
const applySchema = safeCatch(
`applySchema`,
async (collections: Collection_Serialized[]) => {
@ -50,6 +55,7 @@ export const createPbClient = (url: string) => {
client,
url,
knex: rawDb,
createFirstAdmin,
adminAuthViaEmail,
applySchema,
...rpcApi,

View File

@ -5,20 +5,29 @@ import {
PUBLIC_APP_DOMAIN,
PUBLIC_APP_PROTOCOL,
} from '$constants'
import { schema } from '$src/migrate/schema'
import { logger, mkSingleton } from '@pockethost/common'
import { createPbClient } from './PbClient'
export const clientService = mkSingleton(async (url: string) => {
const { dbg, error } = logger().create(`client singleton`)
const client = createPbClient(url)
try {
await client.applySchema(schema)
await client.adminAuthViaEmail(DAEMON_PB_USERNAME, DAEMON_PB_PASSWORD)
dbg(`Logged in`)
} catch (e) {
error(
`***WARNING*** CANNOT AUTHENTICATE TO ${PUBLIC_APP_PROTOCOL}://${PUBLIC_APP_DB}.${PUBLIC_APP_DOMAIN}/_/`
)
error(`***WARNING*** LOG IN MANUALLY, ADJUST .env, AND RESTART DOCKER`)
try {
await client.createFirstAdmin(DAEMON_PB_USERNAME, DAEMON_PB_PASSWORD)
await client.adminAuthViaEmail(DAEMON_PB_USERNAME, DAEMON_PB_PASSWORD)
} catch (e) {
error(
`***WARNING*** CANNOT AUTHENTICATE TO ${PUBLIC_APP_PROTOCOL}://${PUBLIC_APP_DB}.${PUBLIC_APP_DOMAIN}/_/`
)
process.exit(-1)
}
}
return {
client,