chore(auto-admin): plugin.ts

This commit is contained in:
Ben Allfree 2024-06-29 13:54:16 -07:00
parent 7b8248a486
commit b2d33cfdbb
2 changed files with 29 additions and 28 deletions

View File

@ -1,30 +1,3 @@
import PocketBase from 'pocketbase'
import {
PocketHostPlugin,
onAfterInstanceStartedAction,
parseError,
} from 'pockethost'
import { LOGIN, PASSWORD, PLUGIN_NAME } from './constants'
import { dbg, info } from './log'
const plugin: PocketHostPlugin = async ({}) => {
dbg(`initializing ${PLUGIN_NAME}`)
onAfterInstanceStartedAction(async ({ instance, url }) => {
const { subdomain } = instance
dbg(`launched instance ${instance.subdomain} on ${url}`)
const client = new PocketBase(url)
const email = LOGIN()
const password = PASSWORD()
try {
await client.admins.create({ email, password, passwordConfirm: password })
dbg(`Admin account created`)
} catch (e: any) {
dbg(`Failed to create admin account: ${parseError(e)} `)
} finally {
info(`Default admin login for ${subdomain} is ${email}/${password}`)
}
})
}
import { plugin } from './plugin'
export default plugin

View File

@ -0,0 +1,28 @@
import PocketBase from 'pocketbase'
import {
PocketHostPlugin,
onAfterInstanceStartedAction,
parseError,
} from 'pockethost'
import { LOGIN, PASSWORD, PLUGIN_NAME } from './constants'
import { dbg, info } from './log'
export const plugin: PocketHostPlugin = async ({}) => {
dbg(`initializing ${PLUGIN_NAME}`)
onAfterInstanceStartedAction(async ({ instance, url }) => {
const { subdomain } = instance
dbg(`launched instance ${instance.subdomain} on ${url}`)
const client = new PocketBase(url)
const email = LOGIN()
const password = PASSWORD()
try {
await client.admins.create({ email, password, passwordConfirm: password })
dbg(`Admin account created`)
} catch (e: any) {
dbg(`Failed to create admin account: ${parseError(e)} `)
} finally {
info(`Default admin login for ${subdomain} is ${email}/${password}`)
}
})
}