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"
);