Skip to content

Commit

Permalink
feat: improve auth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan committed Sep 19, 2023
1 parent 6a44e9c commit 80cf25b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
5 changes: 4 additions & 1 deletion server/api/auth/verify.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ export default defineProtectedEventHandler(async (event) => {
})
}

return { ok: true }
return {
id: user?.id,
registered: true,
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { users } from '~/server/db/schema'

const registerRequestBody = z.object({
firebaseId: z.string().nonempty(),
email: z.string().email().optional(),
id: z.string().cuid2().optional(),
}).refine(val => !val.email && !val.id, 'Either `email` or `id` must be provided.')
})

export default defineProtectedEventHandler(async (event) => {
const result = await registerRequestBody.safeParseAsync(await readBody(event))
Expand All @@ -19,13 +17,13 @@ export default defineProtectedEventHandler(async (event) => {

const { data } = result

const query = data.id ? eq(users.id, data.id) : eq(users.email, data.email!)

const user = await event.context.database.update(users)
.set({
firebaseId: data.firebaseId,
})
.where(query)
.where(
eq(users.id, event.context.params!.id),
)
.returning()

return user
Expand Down

0 comments on commit 80cf25b

Please # to comment.