fix: resolve OAuth state cookie error - Add proper Cookie configuration with domain settings - Add domain extraction function for production environment - Update environment variables example

This commit is contained in:
theshy
2025-08-01 20:20:03 +08:00
parent da41306918
commit 2cd0ebda65
5 changed files with 162 additions and 13 deletions

View File

@ -4,18 +4,22 @@
console.log("=== 环境变量检查 ===");
const requiredVars = [
'NEXTAUTH_URL',
'NEXTAUTH_SECRET',
'GOOGLE_CLIENT_ID',
'GOOGLE_CLIENT_SECRET',
'DATABASE_URL'
"NEXTAUTH_URL",
"NEXTAUTH_SECRET",
"GOOGLE_CLIENT_ID",
"GOOGLE_CLIENT_SECRET",
"DATABASE_URL",
];
console.log("\n必需的环境变量:");
requiredVars.forEach(varName => {
requiredVars.forEach((varName) => {
const value = process.env[varName];
if (value) {
console.log(`${varName}: ${value.substring(0, 20)}${value.length > 20 ? '...' : ''}`);
console.log(
`${varName}: ${value.substring(0, 20)}${
value.length > 20 ? "..." : ""
}`
);
} else {
console.log(`${varName}: 未设置`);
}
@ -28,14 +32,14 @@ if (nextAuthUrl) {
console.log(`长度: ${nextAuthUrl.length}`);
console.log(`包含引号: ${nextAuthUrl.includes('"')}`);
console.log(`包含单引号: ${nextAuthUrl.includes("'")}`);
// 清理 URL
let cleanUrl = nextAuthUrl.trim();
if (cleanUrl.startsWith('"') && cleanUrl.endsWith('"')) {
cleanUrl = cleanUrl.slice(1, -1);
console.log(`清理后: "${cleanUrl}"`);
}
try {
new URL(cleanUrl);
console.log(`✅ URL 格式有效`);
@ -62,4 +66,6 @@ console.log("\n=== 建议 ===");
console.log("1. 确保 NEXTAUTH_URL 不包含多余的引号");
console.log("2. 确保 Google OAuth 重定向 URI 配置正确");
console.log("3. 在 Google Cloud Console 中添加正确的重定向 URI");
console.log("4. 重定向 URI 格式应为: https://your-domain.com/api/auth/callback/google");
console.log(
"4. 重定向 URI 格式应为: https://your-domain.com/api/auth/callback/google"
);

64
scripts/oauth-debug.js Normal file
View File

@ -0,0 +1,64 @@
#!/usr/bin/env node
// OAuth 调试脚本
console.log("=== OAuth 配置调试 ===");
// 模拟服务器环境变量
const envVars = {
NEXTAUTH_URL: "https://recorder.zyj.best",
GOOGLE_CLIENT_ID:
"1060072115182-l5u59vrbs2lmcpg7pnn72bc8h37eolff.apps.googleusercontent.com",
GOOGLE_CLIENT_SECRET: "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",
};
console.log("\n=== 环境变量检查 ===");
Object.entries(envVars).forEach(([key, value]) => {
console.log(`${key}: ${value.substring(0, 30)}...`);
});
console.log("\n=== 重定向 URI 分析 ===");
const nextAuthUrl = envVars.NEXTAUTH_URL;
const expectedRedirectUri = `${nextAuthUrl}/api/auth/callback/google`;
console.log(`NEXTAUTH_URL: ${nextAuthUrl}`);
console.log(`预期的重定向 URI: ${expectedRedirectUri}`);
// 验证 URL 格式
try {
new URL(nextAuthUrl);
console.log("✅ NEXTAUTH_URL 格式有效");
} catch (error) {
console.log(`❌ NEXTAUTH_URL 格式无效: ${error.message}`);
}
try {
new URL(expectedRedirectUri);
console.log("✅ 重定向 URI 格式有效");
} catch (error) {
console.log(`❌ 重定向 URI 格式无效: ${error.message}`);
}
console.log("\n=== Google Cloud Console 配置检查 ===");
console.log("请在 Google Cloud Console 中验证以下配置:");
console.log("1. 项目 ID: 检查你的 Google Cloud 项目");
console.log(
"2. OAuth 2.0 客户端 ID: 1060072115182-l5u59vrbs2lmcpg7pnn72bc8h37eolff.apps.googleusercontent.com"
);
console.log("3. 授权重定向 URI 应包含:");
console.log(` - ${expectedRedirectUri}`);
console.log("\n=== 常见问题排查 ===");
console.log("1. 确保 Google Cloud Console 中的重定向 URI 完全匹配");
console.log("2. 检查是否有额外的空格或引号");
console.log("3. 确保协议是 https不是 http");
console.log("4. 检查域名是否正确recorder.zyj.best");
console.log("\n=== 测试步骤 ===");
console.log("1. 访问: https://recorder.zyj.best/login");
console.log("2. 点击 '使用 Google 登录'");
console.log("3. 观察浏览器地址栏的重定向 URL");
console.log("4. 检查是否与 Google Cloud Console 中的配置匹配");
console.log("\n=== 调试命令 ===");
console.log("在服务器上运行以下命令查看应用日志:");
console.log("docker logs recorder-app --tail 50");