fix: enhance OAuth configuration and add debugging - Add Google OAuth authorization parameters - Add environment variable validation - Add debugging logs for redirect callback - Create OAuth configuration check script

This commit is contained in:
theshy
2025-08-01 20:42:03 +08:00
parent 2cd0ebda65
commit e955f4c317
8 changed files with 195 additions and 358 deletions

View File

@ -1,119 +0,0 @@
# OAuth 设置指南
## 问题诊断
如果遇到 `Error 400: invalid_request` 错误,通常是 OAuth 配置问题。
### 常见错误原因
1. **NEXTAUTH_URL 格式错误**
- 包含多余的引号
- URL 格式不正确
- 协议不匹配http vs https
2. **Google OAuth 重定向 URI 配置错误**
- 重定向 URI 未在 Google Cloud Console 中正确配置
- 重定向 URI 格式不正确
## 解决步骤
### 1. 检查环境变量
在服务器上运行环境变量检查脚本:
```bash
node scripts/check-env.js
```
### 2. 修复 NEXTAUTH_URL
确保 `.env.production` 文件中的 `NEXTAUTH_URL` 格式正确:
```bash
# 正确的格式
NEXTAUTH_URL="https://recorder.zyj.best"
# 错误的格式(包含多余引号)
NEXTAUTH_URL=""https://recorder.zyj.best""
```
### 3. 配置 Google OAuth
#### 在 Google Cloud Console 中:
1. 访问 [Google Cloud Console](https://console.cloud.google.com/)
2. 选择你的项目
3. 进入 "APIs & Services" > "Credentials"
4. 编辑你的 OAuth 2.0 客户端 ID
5. 在 "Authorized redirect URIs" 中添加:
```
https://recorder.zyj.best/api/auth/callback/google
```
### 4. 验证配置
重新部署应用并测试:
```bash
./deploy.sh
```
### 5. 调试信息
如果问题仍然存在,检查应用日志:
```bash
docker logs recorder-app
```
## 环境变量模板
```bash
# Database
DATABASE_URL="file:./prod.db"
# NextAuth.js
NEXTAUTH_URL="https://recorder.zyj.best"
NEXTAUTH_SECRET="your-secure-secret-here"
# Google OAuth
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"
# AWS S3 Configuration
AWS_ACCESS_KEY_ID="your-aws-access-key-id"
AWS_SECRET_ACCESS_KEY="your-aws-secret-access-key"
AWS_REGION="us-east-1"
AWS_S3_BUCKET="your-s3-bucket-name"
```
## 故障排除
### 错误redirect_uri 格式错误
**原因**NEXTAUTH_URL 包含多余的引号或格式错误
**解决**
1. 检查 `.env.production` 文件
2. 确保 NEXTAUTH_URL 格式正确
3. 重启应用
### 错误redirect_uri 不在授权列表中
**原因**Google OAuth 重定向 URI 未正确配置
**解决**
1. 在 Google Cloud Console 中添加正确的重定向 URI
2. 确保 URI 格式为:`https://your-domain.com/api/auth/callback/google`
### 错误invalid_client
**原因**Google OAuth 凭据错误
**解决**
1. 检查 GOOGLE_CLIENT_ID 和 GOOGLE_CLIENT_SECRET
2. 确保凭据与 Google Cloud Console 中的配置匹配

View File

@ -0,0 +1,100 @@
# 生产环境 OAuth 配置指南
## 🚨 问题描述
在生产环境中Google OAuth 登录出现以下错误:
```
Error 400: invalid_request
Request details: redirect_uri=https://"https//recorder.zyj.best%22/callback/google
```
## 🔍 问题分析
### 根本原因
1. **重定向 URI 格式错误**URI 包含了多余的引号和错误的编码
2. **环境变量配置问题**`NEXTAUTH_URL` 可能没有正确设置
3. **Google Cloud Console 配置问题**:授权重定向 URI 可能不正确
## ✅ 解决方案
### 1. 环境变量配置
确保服务器上的 `.env.production` 文件包含正确的配置:
```bash
# NextAuth.js - 生产环境配置
NEXTAUTH_URL="https://recorder.zyj.best"
NEXTAUTH_SECRET="your-production-nextauth-secret"
# Google OAuth - 生产环境配置
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"
```
### 2. Google Cloud Console 配置
在 Google Cloud Console 中,确保以下重定向 URI 已正确配置:
#### 授权重定向 URI
```
https://recorder.zyj.best/api/auth/callback/google
```
#### JavaScript 来源
```
https://recorder.zyj.best
```
### 3. 验证步骤
1. **检查环境变量**
```bash
# 在服务器上检查环境变量
docker exec -it recorder-app env | grep NEXTAUTH
```
2. **检查 Google Cloud Console**
- 登录 [Google Cloud Console](https://console.cloud.google.com/)
- 进入 "APIs & Services" > "Credentials"
- 检查 OAuth 2.0 客户端 ID 的配置
3. **重启应用**
```bash
./deploy.sh
```
### 4. 调试信息
应用现在包含调试信息,可以在日志中查看重定向过程:
```bash
docker logs recorder-app
```
## 🔧 常见问题
### 问题 1重定向 URI 不匹配
**解决方案**:确保 Google Cloud Console 中的重定向 URI 与 `NEXTAUTH_URL` 一致
### 问题 2HTTPS 证书问题
**解决方案**:确保域名有有效的 SSL 证书
### 问题 3环境变量未生效
**解决方案**:重启 Docker 容器以重新加载环境变量
## 📝 检查清单
- [ ] `NEXTAUTH_URL` 设置为 `https://recorder.zyj.best`
- [ ] Google Cloud Console 中配置了正确的重定向 URI
- [ ] 环境变量文件正确加载
- [ ] 应用已重启
- [ ] SSL 证书有效