From ef022073a2d0f0d7f4cab40b71137d6caf22a256 Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Tue, 21 Feb 2023 07:12:29 -0800 Subject: [PATCH] chore: dev environment fixes --- .env-template | 2 +- packages/common/src/safeCatch.ts | 3 +- packages/daemon/src/migrate/schema.ts | 62 ++----------------- packages/daemon/src/server.ts | 1 - .../src/services/clientService/PbClient.ts | 11 +++- .../services/clientService/clientService.ts | 17 ++++- readme.md | 5 +- 7 files changed, 34 insertions(+), 67 deletions(-) diff --git a/.env-template b/.env-template index e2c2ed32..ac552fad 100644 --- a/.env-template +++ b/.env-template @@ -4,7 +4,7 @@ PUBLIC_APP_DB=pockethost-central DAEMON_PB_BIN_DIR=`pwd`/packages/pocketbase/dist DAEMON_PB_DATA_DIR=`pwd`/.data DAEMON_PB_USERNAME=admin@pockethost.test -DAEMON_PB_PASSWORD=password +DAEMON_PB_PASSWORD=admin@pockethost.test DAEMON_PB_PORT=8090 DAEMON_IDLE_TTL=5000 DAEMON_PB_BACKUP_SLEEP=100 diff --git a/packages/common/src/safeCatch.ts b/packages/common/src/safeCatch.ts index 87457c65..42e823b8 100644 --- a/packages/common/src/safeCatch.ts +++ b/packages/common/src/safeCatch.ts @@ -11,7 +11,7 @@ export const safeCatch = ( const _c = c++ const uuid = `${name}:${_c}` const pfx = `safeCatch:${uuid}` - const { raw, error, warn } = logger().create(pfx) + const { raw, error, warn, dbg } = logger().create(pfx) raw(`args`, args) const tid = setTimeout(() => { error(`timeout ${timeoutMs}ms waiting for ${pfx}`) @@ -29,6 +29,7 @@ export const safeCatch = ( warn( `PocketBase API error: It looks like you don't have permission to make this request.` ) + dbg(JSON.stringify(e, null, 2)) } else if (e.status === 0) { warn(`Client request aborted (duplicate)`) } else { diff --git a/packages/daemon/src/migrate/schema.ts b/packages/daemon/src/migrate/schema.ts index e865cd5e..84f86404 100644 --- a/packages/daemon/src/migrate/schema.ts +++ b/packages/daemon/src/migrate/schema.ts @@ -33,7 +33,7 @@ export const schema: Collection_Serialized[] = [ unique: false, options: { maxSelect: 1, - collectionId: 'systemprofiles0', + collectionId: '_pb_users_auth_', cascadeDelete: false, }, }, @@ -105,62 +105,6 @@ export const schema: Collection_Serialized[] = [ deleteRule: null, options: {}, }, - { - id: 'systemprofiles0', - name: 'users', - type: 'auth', - system: false, - schema: [ - { - id: 'pbfieldname', - name: 'name', - type: 'text', - system: false, - required: false, - unique: false, - options: { - min: null, - max: null, - pattern: '', - }, - }, - { - id: 'pbfieldavatar', - name: 'avatar', - type: 'file', - system: false, - required: false, - unique: false, - options: { - maxSelect: 1, - maxSize: 5242880, - mimeTypes: [ - 'image/jpg', - 'image/jpeg', - 'image/png', - 'image/svg+xml', - 'image/gif', - ], - thumbs: null, - }, - }, - ], - listRule: 'id = @request.auth.id', - viewRule: 'id = @request.auth.id', - createRule: '', - updateRule: 'id = @request.auth.id', - deleteRule: null, - options: { - allowEmailAuth: true, - allowOAuth2Auth: true, - allowUsernameAuth: false, - exceptEmailDomains: null, - manageRule: null, - minPasswordLength: 8, - onlyEmailDomains: null, - requireEmail: true, - }, - }, { id: 'aiw8te7y7atklwn', name: 'invocations', @@ -251,7 +195,7 @@ export const schema: Collection_Serialized[] = [ unique: false, options: { maxSelect: 1, - collectionId: 'systemprofiles0', + collectionId: '_pb_users_auth_', cascadeDelete: false, }, }, @@ -409,3 +353,5 @@ export const schema: Collection_Serialized[] = [ options: {}, }, ] + +console.log(JSON.stringify(schema)) diff --git a/packages/daemon/src/server.ts b/packages/daemon/src/server.ts index 9de84299..ce11a13c 100644 --- a/packages/daemon/src/server.ts +++ b/packages/daemon/src/server.ts @@ -30,7 +30,6 @@ global.EventSource = require('eventsource') await new Promise((resolve) => { exec(`pkill -f 'pocketbase serve'`, (error, stdout, stderr) => { if (error && error.signal !== 'SIGTERM') { - console.log({ error, stderr, stdout }) warn(`pkill failed with ${error}: ${stderr}`) } resolve() diff --git a/packages/daemon/src/services/clientService/PbClient.ts b/packages/daemon/src/services/clientService/PbClient.ts index 06a946f7..35225fa9 100644 --- a/packages/daemon/src/services/clientService/PbClient.ts +++ b/packages/daemon/src/services/clientService/PbClient.ts @@ -35,13 +35,20 @@ export const createPbClient = (url: string) => { const createFirstAdmin = safeCatch( `createFirstAdmin`, - (email: string, password: string) => client.admins.create(email, password) + (email: string, password: string) => + client.admins + .create({ email, password, passwordConfirm: password }) + .catch((res) => { + console.log({ email, password }) + console.log(JSON.stringify(res, null, 2)) + return res + }) ) const applySchema = safeCatch( `applySchema`, async (collections: Collection_Serialized[]) => { - await client.collections.import(collections as Collection[]) + await client.collections.import(collections as Collection[], false) } ) diff --git a/packages/daemon/src/services/clientService/clientService.ts b/packages/daemon/src/services/clientService/clientService.ts index 453c8e57..732a0b6f 100644 --- a/packages/daemon/src/services/clientService/clientService.ts +++ b/packages/daemon/src/services/clientService/clientService.ts @@ -14,21 +14,32 @@ export const clientService = mkSingleton(async (url: string) => { const client = createPbClient(url) try { - await client.applySchema(schema) - await client.adminAuthViaEmail(DAEMON_PB_USERNAME, DAEMON_PB_PASSWORD) dbg(`Logged in`) } catch (e) { + dbg(`Creating first admin account`) + try { await client.createFirstAdmin(DAEMON_PB_USERNAME, DAEMON_PB_PASSWORD) 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}/_/` + `CANNOT AUTHENTICATE TO ${PUBLIC_APP_PROTOCOL}://${PUBLIC_APP_DB}.${PUBLIC_APP_DOMAIN}/_/` ) process.exit(-1) } } + + try { + dbg(`Applying schema`) + await client.applySchema(schema) + dbg(`Schema applied`) + } catch (e) { + error(`Failed to apply base migration schema`) + process.exit(-1) + } + return { client, shutdown() { diff --git a/readme.md b/readme.md index 0f415fe6..1cd6d90f 100644 --- a/readme.md +++ b/readme.md @@ -86,9 +86,12 @@ The entire pockethost.io stack can be run locally. git clone git@github.com:benallfree/pockethost.git cd pockethost yarn -cp .env-template .env # modify as needed +cp .env-template .env # modify as needed, if you used `pockethost.test` for your local domain, everything should work scripts/dev.sh open https://pockethost.test +open https://pockethost-central.pockethost.test + # login: admin@pockethost.test (change in .env) + # password: admin@pockethost.test (change in .env) ``` # Production Deployment