|
| 1 | +import { NextRequest, NextResponse } from 'next/server'; |
| 2 | +import { withRateLimit } from '@/lib/ratelimit'; |
| 3 | +import { exchangeCodeForToken, getGoogleUser, getGoogleAvatarUrl } from '@/lib/google/oauth'; |
| 4 | +import type { AuthResponseDTO, AuthErrorDTO } from '@/types/api/auth.dto'; |
| 5 | +import { edgeLog } from '@/../infra/edge-config'; |
| 6 | + |
| 7 | +export const runtime = 'edge'; |
| 8 | + |
| 9 | +/** |
| 10 | + * GET /api/auth/google/callback |
| 11 | + * Handles Google OAuth2 callback and creates/updates user session |
| 12 | + */ |
| 13 | +export async function GET( |
| 14 | + request: NextRequest, |
| 15 | +): Promise<NextResponse<AuthResponseDTO | AuthErrorDTO>> { |
| 16 | + edgeLog('info', '/api/auth/google/callback', 'GET request received'); |
| 17 | + |
| 18 | + const { addHeaders, rateLimitResponse } = withRateLimit(request, 'AUTH'); |
| 19 | + if (rateLimitResponse) return rateLimitResponse as NextResponse; |
| 20 | + |
| 21 | + try { |
| 22 | + const searchParams = request.nextUrl.searchParams; |
| 23 | + const code = searchParams.get('code'); |
| 24 | + const state = searchParams.get('state'); |
| 25 | + const error = searchParams.get('error'); |
| 26 | + |
| 27 | + // Check for OAuth errors |
| 28 | + if (error) { |
| 29 | + edgeLog('error', '/api/auth/google/callback', `OAuth error: ${error}`); |
| 30 | + return addHeaders( |
| 31 | + NextResponse.json({ message: `Google OAuth error: ${error}` }, { status: 400 }), |
| 32 | + ) as NextResponse; |
| 33 | + } |
| 34 | + |
| 35 | + if (!code) { |
| 36 | + return addHeaders( |
| 37 | + NextResponse.json({ message: 'Authorization code is required' }, { status: 400 }), |
| 38 | + ) as NextResponse; |
| 39 | + } |
| 40 | + |
| 41 | + // Verify state parameter to prevent CSRF attacks |
| 42 | + const storedState = request.cookies.get('google_oauth_state')?.value; |
| 43 | + if (!state || state !== storedState) { |
| 44 | + edgeLog('error', '/api/auth/google/callback', 'Invalid state parameter'); |
| 45 | + return addHeaders( |
| 46 | + NextResponse.json({ message: 'Invalid state parameter' }, { status: 400 }), |
| 47 | + ) as NextResponse; |
| 48 | + } |
| 49 | + |
| 50 | + // Exchange code for access token |
| 51 | + const tokenResponse = await exchangeCodeForToken(code); |
| 52 | + |
| 53 | + // Get Google user information |
| 54 | + const googleUser = await getGoogleUser(tokenResponse.access_token); |
| 55 | + |
| 56 | + // Validate that user has email |
| 57 | + if (!googleUser.email) { |
| 58 | + return addHeaders( |
| 59 | + NextResponse.json( |
| 60 | + { message: 'Google account must have an email' }, |
| 61 | + { status: 400 }, |
| 62 | + ), |
| 63 | + ) as NextResponse; |
| 64 | + } |
| 65 | + |
| 66 | + // Validate email is verified |
| 67 | + if (!googleUser.verified_email) { |
| 68 | + return addHeaders( |
| 69 | + NextResponse.json({ message: 'Google email must be verified' }, { status: 400 }), |
| 70 | + ) as NextResponse; |
| 71 | + } |
| 72 | + |
| 73 | + const mockUserId = Math.random().toString(36).substring(2, 9); |
| 74 | + const mockToken = `mock-jwt-token-${Date.now()}`; |
| 75 | + |
| 76 | + // Clear the state cookie |
| 77 | + const response = NextResponse.json( |
| 78 | + { |
| 79 | + message: 'Google authentication successful', |
| 80 | + user: { |
| 81 | + id: mockUserId, |
| 82 | + name: googleUser.name, |
| 83 | + email: googleUser.email, |
| 84 | + avatar: getGoogleAvatarUrl(googleUser), |
| 85 | + provider: 'google', |
| 86 | + providerId: googleUser.id, |
| 87 | + }, |
| 88 | + token: mockToken, |
| 89 | + }, |
| 90 | + { status: 200 }, |
| 91 | + ); |
| 92 | + |
| 93 | + // Clear the state cookie |
| 94 | + response.cookies.delete('google_oauth_state'); |
| 95 | + |
| 96 | + return addHeaders(response) as NextResponse; |
| 97 | + } catch (error) { |
| 98 | + edgeLog('error', '/api/auth/google/callback', `Error: ${error}`); |
| 99 | + console.error('Google OAuth callback error:', error); |
| 100 | + |
| 101 | + return addHeaders( |
| 102 | + NextResponse.json({ message: 'Internal server error' }, { status: 500 }), |
| 103 | + ) as NextResponse; |
| 104 | + } |
| 105 | +} |
0 commit comments