import 'server-only' /** * Google OAuth (Authorization Code flow), fetch-only — same pattern as * LokiLab. Uses a LokiLoto-dedicated OAuth client (project-specific env var * names, not shared with any other devyouz-lab project's Google client). */ const CLIENT_ID = process.env.LOKILOTO_GOOGLE_OAUTH_CLIENT_ID || '' const CLIENT_SECRET = process.env.LOKILOTO_GOOGLE_OAUTH_CLIENT_SECRET || '' function appUrl(): string { return process.env.NEXT_PUBLIC_SITE_URL || 'https://lotoloki.top' } export const googleEnabled = !!(CLIENT_ID && CLIENT_SECRET) export function googleRedirectUri(): string { return `${appUrl()}/api/auth/google/callback` } export function getGoogleAuthUrl(state: string): string { const params = new URLSearchParams({ client_id: CLIENT_ID, redirect_uri: googleRedirectUri(), response_type: 'code', scope: 'openid email profile', access_type: 'online', prompt: 'select_account', state, }) return `https://accounts.google.com/o/oauth2/v2/auth?${params}` } export interface GoogleProfile { sub: string email: string name: string picture?: string email_verified?: boolean } export async function exchangeGoogleCode(code: string): Promise { const tokenRes = await fetch('https://oauth2.googleapis.com/token', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ code, client_id: CLIENT_ID, client_secret: CLIENT_SECRET, redirect_uri: googleRedirectUri(), grant_type: 'authorization_code', }), }) if (!tokenRes.ok) return null const tokenData = (await tokenRes.json()) as { access_token?: string } if (!tokenData.access_token) return null const profileRes = await fetch('https://www.googleapis.com/oauth2/v3/userinfo', { headers: { Authorization: `Bearer ${tokenData.access_token}` }, }) if (!profileRes.ok) return null return profileRes.json() as Promise }