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

Function POST

freebuff/web/src/app/api/auth/cli/code/route.ts:20–121  ·  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 const existingSession = await db
41 .select({
42 userId: schema.session.userId,
43 expires: schema.session.expires,
44 })
45 .from(schema.session)
46 .where(
47 and(
48 eq(schema.session.fingerprint_id, fingerprintId),
49 gt(schema.session.expires, new Date()),
50 ),
51 )
52 .limit(1)
53
54 if (existingSession.length > 0) {
55 logger.info(
56 {
57 fingerprintId,
58 existingUserId: existingSession[0].userId,
59 event: 'relogin_attempt_with_active_session',
60 },
61 'Login attempt for fingerprint with active session',
62 )
63 }
64
65 const authCode = buildCliAuthCode(
66 fingerprintId,
67 expiresAt.toString(),
68 fingerprintHash,
69 )
70 const loginToken = randomBytes(32).toString('base64url')
71
72 await db.insert(schema.verificationToken).values({
73 identifier: getCliAuthCodeTokenIdentifier(loginToken),
74 token: authCode,
75 expires: new Date(expiresAt),
76 })
77

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