Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 7x 7x 7x 7x 7x 7x 8x 8x 8x 8x 8x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x | import { createId } from '@paralleldrive/cuid2'
import { eq } from 'drizzle-orm'
import { db, schema } from '@/db'
/**
* Upgrade a guest user to a full account.
*
* Updates the existing guest user row with email, name, image, and upgradedAt.
* Links the OAuth/email provider in auth_accounts.
*
* @returns The upgraded user's database ID
*/
export async function upgradeGuestToUser(opts: {
guestId: string
email: string
name?: string | null
image?: string | null
provider: string
providerAccountId: string
providerType: string
}): Promise<string | null> {
const guestUser = await db.query.users.findFirst({
where: eq(schema.users.guestId, opts.guestId),
})
if (!guestUser) {
console.warn(`[auth] upgradeGuestToUser: no user found for guestId=${opts.guestId}`)
return null
}
// Update the guest user row with account info
await db
.update(schema.users)
.set({
email: opts.email,
name: opts.name ?? guestUser.name,
image: opts.image ?? guestUser.image,
upgradedAt: new Date(),
})
.where(eq(schema.users.id, guestUser.id))
// Link the provider
await db.insert(schema.authAccounts).values({
id: createId(),
userId: guestUser.id,
provider: opts.provider,
providerAccountId: opts.providerAccountId,
type: opts.providerType,
})
console.log(
`[auth] upgraded guest ${guestUser.id} to full account via ${opts.provider} (${opts.email})`
)
return guestUser.id
}
|