mirror of
https://github.com/pockethost/pockethost.git
synced 2026-03-15 21:05:05 +00:00
fix: move admin sync to instance app
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"typeRoots": ["./types"]
|
||||
}
|
||||
}
|
||||
55
src/instance-app/pb_hooks/_ph_admin_sync.pb.js
Normal file
55
src/instance-app/pb_hooks/_ph_admin_sync.pb.js
Normal file
@@ -0,0 +1,55 @@
|
||||
onAfterBootstrap((e) => {
|
||||
const dao = $app.dao()
|
||||
const { mkLog } = /** @type {Lib} */ (require(`${__hooks}/_ph_lib.js`))
|
||||
|
||||
const log = mkLog(`admin-sync`)
|
||||
|
||||
const { email, tokenKey, passwordHash } = JSON.parse($os.getenv(`ADMIN_SYNC`))
|
||||
|
||||
if (!email) {
|
||||
log(`Not active - skipped`)
|
||||
}
|
||||
|
||||
const result = new DynamicModel({
|
||||
// describe the shape of the data (used also as initial values)
|
||||
id: '',
|
||||
})
|
||||
|
||||
try {
|
||||
dao
|
||||
.db()
|
||||
.newQuery('SELECT * from _admins where email = {:email}')
|
||||
.bind({ email })
|
||||
.one(result)
|
||||
log(
|
||||
`Existing admin record matching PocketHost login found - updating with latest credentials`,
|
||||
)
|
||||
try {
|
||||
dao
|
||||
.db()
|
||||
.newQuery(
|
||||
'update _admins set tokenKey={:tokenKey}, passwordHash={:passwordHash} where email={:email}',
|
||||
)
|
||||
.bind({ email, tokenKey, passwordHash })
|
||||
.execute()
|
||||
log(`Success`)
|
||||
} catch (e) {
|
||||
log(`Failed to update admin credentials: ${e}`)
|
||||
}
|
||||
} catch (e) {
|
||||
log(`No admin record matching PocketHost credentials - creating`)
|
||||
|
||||
try {
|
||||
dao
|
||||
.db()
|
||||
.newQuery(
|
||||
'insert into _admins (email, tokenKey, passwordHash) VALUES ({:email}, {:tokenKey}, {:passwordHash})',
|
||||
)
|
||||
.bind({ email, tokenKey, passwordHash })
|
||||
.execute()
|
||||
log(`Success`)
|
||||
} catch (e) {
|
||||
log(`Failed to insert admin credentials: ${e}`)
|
||||
}
|
||||
}
|
||||
})
|
||||
19
src/instance-app/pb_hooks/_ph_lib.js
Normal file
19
src/instance-app/pb_hooks/_ph_lib.js
Normal file
@@ -0,0 +1,19 @@
|
||||
/** @type {Lib['mkLog']} */
|
||||
const mkLog =
|
||||
(namespace) =>
|
||||
/**
|
||||
* @param {...any} s
|
||||
* @returns
|
||||
*/
|
||||
(...s) =>
|
||||
console.log(
|
||||
`[${namespace}]`,
|
||||
...s.map((p) => {
|
||||
if (typeof p === 'object') return JSON.stringify(p, null, 2)
|
||||
return p
|
||||
}),
|
||||
)
|
||||
|
||||
module.exports = {
|
||||
mkLog,
|
||||
}
|
||||
8
src/instance-app/tsconfig.json
Normal file
8
src/instance-app/tsconfig.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"noImplicitAny": false,
|
||||
"strictNullChecks": false
|
||||
},
|
||||
"include": ["pb_hooks", "types"]
|
||||
}
|
||||
5
src/instance-app/types/lib.d.ts
vendored
Normal file
5
src/instance-app/types/lib.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
type Logger = (...args: any[]) => void
|
||||
|
||||
interface Lib {
|
||||
mkLog: (namespace: string) => Logger
|
||||
}
|
||||
10400
src/instance-app/types/types.d.ts
vendored
10400
src/instance-app/types/types.d.ts
vendored
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user