forked from 77media/video-flow
api/auth/google/callback改调用java
This commit is contained in:
parent
dcc3044bdd
commit
4dc67a4507
@ -170,7 +170,7 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
// 第二步:使用id_token调用Java后端
|
||||
const javaBaseUrl = process.env.NEXT_PUBLIC_JAVA_URL || 'https://77.app.java.auth.qikongjian.com';
|
||||
const javaBaseUrl = process.env.NEXT_PUBLIC_JAVA_URL || 'https://auth.test.movieflow.ai';
|
||||
console.log('开始调用Java后端:', javaBaseUrl);
|
||||
|
||||
const backendResponse = await fetch(`${javaBaseUrl}/api/auth/google/login`, {
|
||||
|
||||
@ -59,20 +59,41 @@ export default function OAuthCallback() {
|
||||
console.warn('无法解析state参数:', params.state, e);
|
||||
}
|
||||
|
||||
// 从 sessionStorage 获取邀请码
|
||||
let inviteCode: string | undefined = undefined;
|
||||
try {
|
||||
const sessionInviteCode = sessionStorage.getItem("inviteCode");
|
||||
if (sessionInviteCode) {
|
||||
inviteCode = sessionInviteCode;
|
||||
console.log('从 sessionStorage 获取到邀请码:', inviteCode);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('无法从 sessionStorage 获取邀请码:', e);
|
||||
}
|
||||
|
||||
// 优先级:sessionStorage > state参数 > undefined
|
||||
const finalInviteCode = inviteCode || stateData.inviteCode || undefined;
|
||||
|
||||
console.log('开始处理Google OAuth回调, code:', params.code?.substring(0, 20) + '...');
|
||||
console.log('State数据:', stateData);
|
||||
console.log('最终使用的邀请码:', finalInviteCode);
|
||||
|
||||
// 调用后端处理授权码
|
||||
const response = await fetch('/api/auth/google/callback', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
// 调用后端处理授权码 - 直接调用Java后端
|
||||
const JAVA_BASE_URL = process.env.NEXT_PUBLIC_JAVA_URL || 'https://auth.test.movieflow.ai';
|
||||
const queryParams = new URLSearchParams({
|
||||
code: params.code,
|
||||
state: params.state,
|
||||
inviteCode: stateData.inviteCode || undefined
|
||||
})
|
||||
...(finalInviteCode && { inviteCode: finalInviteCode })
|
||||
});
|
||||
|
||||
console.log('调用Java后端OAuth接口:', `${JAVA_BASE_URL}/api/auth/google/callback`);
|
||||
|
||||
const response = await fetch(`${JAVA_BASE_URL}/api/auth/google/callback?${queryParams.toString()}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
25
lib/auth.ts
25
lib/auth.ts
@ -234,9 +234,24 @@ export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
||||
try {
|
||||
console.log('开始Google登录流程,使用环境变量配置...');
|
||||
|
||||
// 从环境变量获取配置(临时硬编码确保正确)
|
||||
// 从 sessionStorage 获取邀请码(如果没有传入的话)
|
||||
let finalInviteCode = inviteCode;
|
||||
if (!finalInviteCode) {
|
||||
try {
|
||||
const sessionInviteCode = sessionStorage.getItem("inviteCode");
|
||||
if (sessionInviteCode) {
|
||||
finalInviteCode = sessionInviteCode;
|
||||
console.log('从 sessionStorage 获取到邀请码:', finalInviteCode);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('无法从 sessionStorage 获取邀请码:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 从环境变量获取配置
|
||||
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';
|
||||
const javaBaseUrl = process.env.NEXT_PUBLIC_JAVA_URL || 'https://auth.test.movieflow.ai';
|
||||
const redirectUri = process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI || `${javaBaseUrl}/api/auth/google/callback`;
|
||||
|
||||
// 生成随机nonce用于安全验证
|
||||
const nonce = Array.from(crypto.getRandomValues(new Uint8Array(32)))
|
||||
@ -244,7 +259,7 @@ export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
||||
|
||||
// 构建state参数(包含邀请码等信息)
|
||||
const stateData = {
|
||||
inviteCode: inviteCode || '',
|
||||
inviteCode: finalInviteCode || '',
|
||||
timestamp: Date.now(),
|
||||
origin: window.location.pathname + window.location.search,
|
||||
nonce: nonce
|
||||
@ -252,8 +267,10 @@ export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
||||
|
||||
console.log('使用的配置:', {
|
||||
clientId,
|
||||
javaBaseUrl,
|
||||
redirectUri,
|
||||
envClientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
|
||||
envJavaUrl: process.env.NEXT_PUBLIC_JAVA_URL,
|
||||
envRedirectUri: process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI
|
||||
});
|
||||
|
||||
@ -299,7 +316,7 @@ export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
||||
sessionStorage.setItem('google_oauth_state', JSON.stringify({
|
||||
nonce: nonce,
|
||||
timestamp: Date.now(),
|
||||
inviteCode: inviteCode || ''
|
||||
inviteCode: finalInviteCode || ''
|
||||
}));
|
||||
|
||||
// 直接在当前页面跳转到Google
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user