(req: Request)
| 18 | import { getLoginUrlOrigin } from './_origin' |
| 19 | |
| 20 | export 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 |
nothing calls this directly
no test coverage detected