MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / POST

Function POST

web/src/app/api/auth/cli/code/route.ts:20–123  ·  view source on GitHub ↗
(req: Request)

Source from the content-addressed store, hash-verified

18import { getLoginUrlOrigin } from './_origin'
19
20export async function POST(req: Request) {
21 const reqSchema = z.object({
22 fingerprintId: z.string(),
23 })
24 const requestBody = await req.json()
25 const result = reqSchema.safeParse(requestBody)
26 if (!result.success) {
27 return NextResponse.json({ error: 'Invalid request body' }, { status: 400 })
28 }
29
30 const { fingerprintId } = result.data
31
32 try {
33 const expiresAt = Date.now() + 60 * 60 * 1000 // 1 hour
34 const fingerprintHash = genAuthCode(
35 fingerprintId,
36 expiresAt.toString(),
37 env.NEXTAUTH_SECRET,
38 )
39
40 // Check if this fingerprint has any active sessions
41 const existingSession = await db
42 .select({
43 userId: schema.session.userId,
44 expires: schema.session.expires,
45 })
46 .from(schema.session)
47 .where(
48 and(
49 eq(schema.session.fingerprint_id, fingerprintId),
50 gt(schema.session.expires, new Date()),
51 ),
52 )
53 .limit(1)
54
55 if (existingSession.length > 0) {
56 // There's an active session - log this for monitoring
57 logger.info(
58 {
59 fingerprintId,
60 existingUserId: existingSession[0].userId,
61 event: 'relogin_attempt_with_active_session',
62 },
63 'Login attempt for fingerprint with active session',
64 )
65 }
66
67 const authCode = buildCliAuthCode(
68 fingerprintId,
69 expiresAt.toString(),
70 fingerprintHash,
71 )
72 const loginToken = randomBytes(32).toString('base64url')
73
74 await db.insert(schema.verificationToken).values({
75 identifier: getCliAuthCodeTokenIdentifier(loginToken),
76 token: authCode,
77 expires: new Date(expiresAt),

Callers

nothing calls this directly

Calls 9

genAuthCodeFunction · 0.90
buildCliAuthCodeFunction · 0.90
getLoginUrlOriginFunction · 0.90
getCliAuthCodeHashPrefixFunction · 0.90
fromMethod · 0.80
insertMethod · 0.80
setMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected