mirror your GitHub repos to tangled.org automatically
1import { userIdentity } from '~~/server/db/schema'
2
3export default defineEventHandler(async event => {
4 const url = getRequestURL(event)
5 const params = url.searchParams
6
7 const client = await useOAuthClient()
8 const { session, state } = await client.callback(params)
9
10 const installationId = state ? Number(state) : NaN
11 if (!Number.isFinite(installationId)) {
12 throw createError({ statusCode: 400, statusMessage: 'invalid state (missing installation id)' })
13 }
14
15 const db = useDb()
16 await db.insert(userIdentity).values({
17 did: session.did,
18 handle: null, // resolved separately; we don't have it from the session blob
19 installationId,
20 updatedAt: new Date(),
21 }).onConflictDoUpdate({
22 target: userIdentity.did,
23 set: { installationId, updatedAt: new Date() },
24 })
25
26 await sendRedirect(event, '/dashboard', 302)
27})