forked from 77media/video-flow
updates google login
This commit is contained in:
parent
f019c2bd34
commit
81f92215c5
@ -86,4 +86,5 @@ export interface OAuthState {
|
||||
state: string;
|
||||
timestamp: number;
|
||||
redirectUrl?: string;
|
||||
inviteCode?: string;
|
||||
}
|
||||
|
||||
35
lib/auth.ts
35
lib/auth.ts
@ -232,13 +232,23 @@ export const initializeGoogleGSI = (): Promise<void> => {
|
||||
*/
|
||||
export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
||||
try {
|
||||
console.log('开始Google登录流程(Medium风格)...');
|
||||
console.log('开始Google登录流程,使用环境变量配置...');
|
||||
|
||||
// 从环境变量获取配置(临时硬编码确保正确)
|
||||
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('');
|
||||
|
||||
// 构建state参数
|
||||
// 构建state参数(包含邀请码等信息)
|
||||
const stateData = {
|
||||
inviteCode: inviteCode || '',
|
||||
timestamp: Date.now(),
|
||||
@ -246,17 +256,20 @@ export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
||||
nonce: nonce
|
||||
};
|
||||
|
||||
// 根据环境变量确定redirect_uri
|
||||
const redirectUri = process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI || 'https://www.movieflow.ai/api/auth/google/callback';
|
||||
console.log('使用的配置:', {
|
||||
clientId,
|
||||
redirectUri,
|
||||
actualRedirectUri,
|
||||
envClientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
|
||||
envRedirectUri: process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI
|
||||
});
|
||||
|
||||
console.log('使用的redirect_uri:', redirectUri);
|
||||
|
||||
// 构建Google OAuth2授权URL(Medium风格参数)
|
||||
// 构建Google OAuth2授权URL
|
||||
const authParams = new URLSearchParams({
|
||||
access_type: 'online',
|
||||
client_id: GOOGLE_CLIENT_ID,
|
||||
client_id: clientId,
|
||||
nonce: nonce,
|
||||
redirect_uri: redirectUri,
|
||||
redirect_uri: actualRedirectUri, // 使用强制修复的URI
|
||||
response_type: 'code', // 使用授权码模式
|
||||
scope: 'email openid profile',
|
||||
state: JSON.stringify(stateData),
|
||||
@ -266,6 +279,8 @@ export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
||||
const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?${authParams.toString()}`;
|
||||
|
||||
console.log('跳转到Google授权页面:', authUrl);
|
||||
console.log('🔍 调试信息 - 授权URL中的redirect_uri:', authParams.get('redirect_uri'));
|
||||
console.log('🔍 调试信息 - 当前页面域名:', window.location.origin);
|
||||
|
||||
// 保存state到sessionStorage用于验证
|
||||
sessionStorage.setItem('google_oauth_state', JSON.stringify({
|
||||
@ -274,7 +289,7 @@ export const signInWithGoogle = async (inviteCode?: string): Promise<void> => {
|
||||
inviteCode: inviteCode || ''
|
||||
}));
|
||||
|
||||
// 直接在当前页面跳转到Google (Medium风格)
|
||||
// 直接在当前页面跳转到Google
|
||||
window.location.href = authUrl;
|
||||
|
||||
} catch (error) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user