From f71c46883eb3b2ab9920af61cc146dfc034780c8 Mon Sep 17 00:00:00 2001 From: Zixin Zhou Date: Mon, 22 Sep 2025 13:16:20 +0800 Subject: [PATCH] updates google login --- app/api/auth/google/callback/route.ts | 14 +++++++++++++- lib/auth.ts | 11 ++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/api/auth/google/callback/route.ts b/app/api/auth/google/callback/route.ts index f7264c5..b077e95 100644 --- a/app/api/auth/google/callback/route.ts +++ b/app/api/auth/google/callback/route.ts @@ -281,5 +281,17 @@ export async function GET(request: NextRequest) { // 重定向到页面路由,让页面处理OAuth回调 const callbackUrl = `/users/oauth/callback?code=${encodeURIComponent(code)}&state=${encodeURIComponent(state)}`; - return NextResponse.redirect(new URL(callbackUrl, request.url)); + + // 修复:确保使用正确的域名进行重定向 + const host = request.headers.get('host') || 'www.movieflow.net'; + const protocol = request.headers.get('x-forwarded-proto') || 'https'; + const fullCallbackUrl = `${protocol}://${host}${callbackUrl}`; + + console.log('🔍 前端API重定向调试:'); + console.log(' - request.url:', request.url); + console.log(' - host header:', host); + console.log(' - protocol:', protocol); + console.log(' - 重定向到:', fullCallbackUrl); + + return NextResponse.redirect(fullCallbackUrl); } diff --git a/lib/auth.ts b/lib/auth.ts index 941f367..a3c8058 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -237,13 +237,7 @@ export const signInWithGoogle = async (inviteCode?: string): Promise => { // 从环境变量获取配置(临时硬编码确保正确) const clientId = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || '847079918888-o1nne8d3ij80dn20qurivo987pv07225.apps.googleusercontent.com'; const redirectUri = process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI || 'https://www.movieflow.net/api/auth/google/callback'; - - // 根据当前域名选择正确的redirect_uri - const isLocalhost = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'; - const actualRedirectUri = isLocalhost - ? 'http://localhost:3000/api/auth/google/callback' // 本地开发 - : redirectUri; // 使用环境变量配置 - + // 生成随机nonce用于安全验证 const nonce = Array.from(crypto.getRandomValues(new Uint8Array(32))) .map(b => b.toString(16).padStart(2, '0')).join(''); @@ -259,7 +253,6 @@ export const signInWithGoogle = async (inviteCode?: string): Promise => { console.log('使用的配置:', { clientId, redirectUri, - actualRedirectUri, envClientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID, envRedirectUri: process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI }); @@ -279,7 +272,7 @@ export const signInWithGoogle = async (inviteCode?: string): Promise => { access_type: 'online', client_id: clientId, nonce: nonce, - redirect_uri: actualRedirectUri, // 使用强制修复的URI + redirect_uri: redirectUri, response_type: 'code', // 使用授权码模式 scope: 'email openid profile', state: JSON.stringify(stateData),