File size: 1,213 Bytes
eeb9404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextRequest, NextResponse } from 'next/server';
import { CODEX_COOKIE_NAME, codexCookieOptions } from '../cookie';

/**
 * Receives the full Codex auth JSON from the client, stores the refresh_token
 * in an HttpOnly cookie, and returns the non-sensitive fields.
 */
export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const { access_token, refresh_token, expires_at, user_email } = body;

    if (!refresh_token || typeof refresh_token !== 'string') {
      return NextResponse.json({ error: 'Missing refresh_token' }, { status: 400 });
    }
    if (!access_token || typeof access_token !== 'string') {
      return NextResponse.json({ error: 'Missing access_token' }, { status: 400 });
    }

    const response = NextResponse.json({
      access_token,
      expires_at: expires_at || Math.floor(Date.now() / 1000) + 3600,
      user_email: user_email || undefined,
    });

    response.cookies.set(CODEX_COOKIE_NAME, refresh_token, codexCookieOptions());

    return response;
  } catch (error) {
    const message = error instanceof Error ? error.message : 'Unknown error';
    return NextResponse.json({ error: message }, { status: 500 });
  }
}