Skip to content

Commit

Permalink
chore: create admin after migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Nov 24, 2024
1 parent e34159f commit 6b0d772
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 51 deletions.
8 changes: 6 additions & 2 deletions apps/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ const openapi = container.resolve(OpenAPI)
const opentelemetry = container.resolve(OpenTelemetryModule)
const template = container.resolve(TemplateModule)

const logger = createLogger("app")

export const app = new Elysia()
.onStart(async () => {
.on("start", async () => {
logger.info("db migrate start")
await dbMigrate()
logger.info("db migrate done")
await auth.onStart()
})
.use(opentelemetry.plugin())
.use(loggerPlugin())
Expand All @@ -64,7 +69,6 @@ export const app = new Elysia()
set.headers["Server-Timing"] = `handle;dur=${(await end) - begin}`
})
.onStart(async () => {
const logger = createLogger("app onstart")
const pubsub = container.resolve(PubSubContext)
const webhookEventHandler = container.resolve(WebhookEventsHandler)
// const auditEventHandler = container.resolve(AuditEventHandler)
Expand Down
100 changes: 51 additions & 49 deletions apps/backend/src/modules/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,62 +173,64 @@ export class Auth {
}
}

route() {
const oauth = container.resolve(OAuth)
return new Elysia()
.use(oauth.route())
.onStart(async (ctx) => {
const adminEmail = env.UNDB_ADMIN_EMAIL
const adminPassword = env.UNDB_ADMIN_PASSWORD
if (env.UNDB_DISABLE_REGISTRATION) {
this.logger.info("Registration is disabled")
if (!adminEmail || !adminPassword) {
const message =
"Registration is disabled but admin user is not set, please set UNDB_ADMIN_EMAIL and UNDB_ADMIN_PASSWORD environment variables"
this.logger.fatal(message)
throw new Error(message)
}
async onStart() {
const adminEmail = env.UNDB_ADMIN_EMAIL
const adminPassword = env.UNDB_ADMIN_PASSWORD
if (env.UNDB_DISABLE_REGISTRATION) {
this.logger.info("Registration is disabled")
if (!adminEmail || !adminPassword) {
const message =
"Registration is disabled but admin user is not set, please set UNDB_ADMIN_EMAIL and UNDB_ADMIN_PASSWORD environment variables"
this.logger.fatal(message)
throw new Error(message)
}

const user = await this.queryBuilder
.selectFrom("undb_user")
.selectAll()
.where((eb) => eb.eb("email", "=", adminEmail))
.executeTakeFirst()
const user = await this.queryBuilder
.selectFrom("undb_user")
.selectAll()
.where((eb) => eb.eb("email", "=", adminEmail))
.executeTakeFirst()

if (!user) {
const userId = generateIdFromEntropySize(10) // 16 characters long
const passwordHash = await Bun.password.hash(adminPassword)
const username = getUsernameFromEmail(adminEmail)
if (!user) {
this.logger.info("Admin user not found, creating...")
const userId = generateIdFromEntropySize(10) // 16 characters long
const passwordHash = await Bun.password.hash(adminPassword)
const username = getUsernameFromEmail(adminEmail)

executionContext.enterWith({
requestId: v7(),
user: {
userId,
username,
email: adminEmail,
},
})

executionContext.enterWith({
requestId: v7(),
user: {
userId,
username,
email: adminEmail,
},
await withTransaction(this.queryBuilder)(async () => {
await getCurrentTransaction()
.insertInto("undb_user")
.values({
email: adminEmail,
username: username!,
id: userId,
password: passwordHash,
email_verified: true,
})
.execute()

await withTransaction(this.queryBuilder)(async () => {
await getCurrentTransaction()
.insertInto("undb_user")
.values({
email: adminEmail,
username: username!,
id: userId,
password: passwordHash,
email_verified: true,
})
.execute()
const space = await this.spaceService.createSpace({ name: username! })
await this.spaceMemberService.createMember(userId, space.id.value, "owner")

const space = await this.spaceService.createSpace({ name: username! })
await this.spaceMemberService.createMember(userId, space.id.value, "owner")
this.logger.info("Admin user created")
})
}
}
}

this.logger.info("Admin user created")
})
}
}
})
route() {
const oauth = container.resolve(OAuth)
return new Elysia()
.use(oauth.route())
.onAfterResponse((ctx) => {
const requestId = executionContext.getStore()?.requestId
this.logger.info(
Expand Down

0 comments on commit 6b0d772

Please sign in to comment.