2025-09-22 22:40:47 +08:00

64 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from 'next/server';
/**
* Google OAuth回调处理API
* 注意POST 方法已废弃,业务逻辑已移至 /users/oauth/callback 页面
* 此文件现在仅用于处理 Google 的 GET 回调重定向
*/
/**
* 根据请求获取正确的redirect_uri
*/
function getRedirectUri(request: NextRequest): string {
const host = request.headers.get('host') || '';
if (host.includes('localhost') || host.includes('127.0.0.1')) {
// 本地开发环境使用实际的端口号可能是3000或3001
const protocol = 'http';
return `${protocol}://${host}/api/auth/google/callback`;
} else if (host.includes('movieflow.net')) {
return 'https://www.movieflow.net/api/auth/google/callback';
} else if (host.includes('movieflow.ai')) {
return 'https://www.movieflow.ai/api/auth/google/callback';
} else {
// 默认使用生产环境
return 'https://www.movieflow.ai/api/auth/google/callback';
}
}
/**
* 处理GET请求 - Google OAuth回调
* 将GET请求重定向到页面路由进行处理
*/
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const code = searchParams.get('code');
const state = searchParams.get('state');
if (!code || !state) {
return NextResponse.json(
{
success: false,
message: 'Missing required parameters: code and state'
},
{ status: 400 }
);
}
// 重定向到页面路由让页面处理OAuth回调
const callbackUrl = `/users/oauth/callback?code=${encodeURIComponent(code)}&state=${encodeURIComponent(state)}`;
// 修复:确保使用正确的域名进行重定向
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);
}