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后端
|
// 第二步:使用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);
|
console.log('开始调用Java后端:', javaBaseUrl);
|
||||||
|
|
||||||
const backendResponse = await fetch(`${javaBaseUrl}/api/auth/google/login`, {
|
const backendResponse = await fetch(`${javaBaseUrl}/api/auth/google/login`, {
|
||||||
|
|||||||
@ -59,20 +59,41 @@ export default function OAuthCallback() {
|
|||||||
console.warn('无法解析state参数:', params.state, e);
|
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('开始处理Google OAuth回调, code:', params.code?.substring(0, 20) + '...');
|
||||||
console.log('State数据:', stateData);
|
console.log('State数据:', stateData);
|
||||||
|
console.log('最终使用的邀请码:', finalInviteCode);
|
||||||
|
|
||||||
// 调用后端处理授权码
|
// 调用后端处理授权码 - 直接调用Java后端
|
||||||
const response = await fetch('/api/auth/google/callback', {
|
const JAVA_BASE_URL = process.env.NEXT_PUBLIC_JAVA_URL || 'https://auth.test.movieflow.ai';
|
||||||
method: 'POST',
|
const queryParams = new URLSearchParams({
|
||||||
|
code: params.code,
|
||||||
|
state: params.state,
|
||||||
|
...(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: {
|
headers: {
|
||||||
|
'Accept': 'application/json',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
}
|
||||||
body: JSON.stringify({
|
|
||||||
code: params.code,
|
|
||||||
state: params.state,
|
|
||||||
inviteCode: stateData.inviteCode || undefined
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|||||||
29
lib/auth.ts
29
lib/auth.ts
@ -234,9 +234,24 @@ export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
|||||||
try {
|
try {
|
||||||
console.log('开始Google登录流程,使用环境变量配置...');
|
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 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用于安全验证
|
// 生成随机nonce用于安全验证
|
||||||
const nonce = Array.from(crypto.getRandomValues(new Uint8Array(32)))
|
const nonce = Array.from(crypto.getRandomValues(new Uint8Array(32)))
|
||||||
@ -244,16 +259,18 @@ export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
|||||||
|
|
||||||
// 构建state参数(包含邀请码等信息)
|
// 构建state参数(包含邀请码等信息)
|
||||||
const stateData = {
|
const stateData = {
|
||||||
inviteCode: inviteCode || '',
|
inviteCode: finalInviteCode || '',
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
origin: window.location.pathname + window.location.search,
|
origin: window.location.pathname + window.location.search,
|
||||||
nonce: nonce
|
nonce: nonce
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('使用的配置:', {
|
console.log('使用的配置:', {
|
||||||
clientId,
|
clientId,
|
||||||
|
javaBaseUrl,
|
||||||
redirectUri,
|
redirectUri,
|
||||||
envClientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
|
envClientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
|
||||||
|
envJavaUrl: process.env.NEXT_PUBLIC_JAVA_URL,
|
||||||
envRedirectUri: process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI
|
envRedirectUri: process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -290,7 +307,7 @@ export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
|||||||
authParams.forEach((value, key) => {
|
authParams.forEach((value, key) => {
|
||||||
if (key === 'state') {
|
if (key === 'state') {
|
||||||
console.log(` - ${key}: ${JSON.stringify(JSON.parse(value), null, 2)}`);
|
console.log(` - ${key}: ${JSON.stringify(JSON.parse(value), null, 2)}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(` - ${key}: ${value}`);
|
console.log(` - ${key}: ${value}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -299,7 +316,7 @@ export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
|||||||
sessionStorage.setItem('google_oauth_state', JSON.stringify({
|
sessionStorage.setItem('google_oauth_state', JSON.stringify({
|
||||||
nonce: nonce,
|
nonce: nonce,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inviteCode: inviteCode || ''
|
inviteCode: finalInviteCode || ''
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// 直接在当前页面跳转到Google
|
// 直接在当前页面跳转到Google
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user