mirror of
https://github.com/hanzoai/ui.git
synced 2026-11-20 22:36:27 +00:00
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { cookies } from 'next/headers'
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { createSessionCookie, revokeAllSessions } from './firebase-support'
|
|
import type { APIResponse } from '../types'
|
|
|
|
export async function handleLogin(request: NextRequest) {
|
|
const reqBody = (await request.json()) as { idToken: string }
|
|
const idToken = reqBody.idToken
|
|
|
|
const expiresIn = 60 * 60 * 24 * 5 * 1000 // 5 days
|
|
|
|
const sessionCookie = await createSessionCookie(idToken, { expiresIn })
|
|
|
|
const c = await cookies()
|
|
c.set('__session', sessionCookie, { maxAge: expiresIn, httpOnly: true, secure: true })
|
|
|
|
return NextResponse.json<APIResponse<string>>({ success: true, data: 'Signed in successfully.' })
|
|
}
|
|
|
|
export async function handleLogout() {
|
|
const c = await cookies()
|
|
const sessionCookie = c.get('__session')?.value
|
|
|
|
if (!sessionCookie) {
|
|
return NextResponse.json<APIResponse<string>>({ success: false, error: 'Session not found.' }, { status: 400 })
|
|
}
|
|
|
|
c.delete('__session')
|
|
|
|
await revokeAllSessions(sessionCookie)
|
|
|
|
return NextResponse.json<APIResponse<string>>({ success: true, data: 'Signed out successfully.' })
|
|
}
|