fix: resolve OAuth redirect_uri error - Add URL validation and cleanup in auth config - Create environment variable check script - Add OAuth setup guide documentation
This commit is contained in:
65
scripts/check-env.js
Normal file
65
scripts/check-env.js
Normal file
@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
// 环境变量检查脚本
|
||||
console.log("=== 环境变量检查 ===");
|
||||
|
||||
const requiredVars = [
|
||||
'NEXTAUTH_URL',
|
||||
'NEXTAUTH_SECRET',
|
||||
'GOOGLE_CLIENT_ID',
|
||||
'GOOGLE_CLIENT_SECRET',
|
||||
'DATABASE_URL'
|
||||
];
|
||||
|
||||
console.log("\n必需的环境变量:");
|
||||
requiredVars.forEach(varName => {
|
||||
const value = process.env[varName];
|
||||
if (value) {
|
||||
console.log(`✅ ${varName}: ${value.substring(0, 20)}${value.length > 20 ? '...' : ''}`);
|
||||
} else {
|
||||
console.log(`❌ ${varName}: 未设置`);
|
||||
}
|
||||
});
|
||||
|
||||
console.log("\n=== NEXTAUTH_URL 详细检查 ===");
|
||||
const nextAuthUrl = process.env.NEXTAUTH_URL;
|
||||
if (nextAuthUrl) {
|
||||
console.log(`原始值: "${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 格式有效`);
|
||||
} catch (error) {
|
||||
console.log(`❌ URL 格式无效: ${error.message}`);
|
||||
}
|
||||
} else {
|
||||
console.log("❌ NEXTAUTH_URL 未设置");
|
||||
}
|
||||
|
||||
console.log("\n=== Google OAuth 配置检查 ===");
|
||||
const googleClientId = process.env.GOOGLE_CLIENT_ID;
|
||||
const googleClientSecret = process.env.GOOGLE_CLIENT_SECRET;
|
||||
|
||||
if (googleClientId && googleClientSecret) {
|
||||
console.log("✅ Google OAuth 凭据已设置");
|
||||
console.log(`Client ID 长度: ${googleClientId.length}`);
|
||||
console.log(`Client Secret 长度: ${googleClientSecret.length}`);
|
||||
} else {
|
||||
console.log("❌ Google OAuth 凭据未完全设置");
|
||||
}
|
||||
|
||||
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");
|
||||
Reference in New Issue
Block a user