- 添加完整的项目基础结构,包括配置、类型定义和常量 - 实现OAuth认证流程和令牌管理 - 开发请求转换和响应处理逻辑 - 添加SSE流处理和ChatCompletions API转换 - 实现模型映射和提示指令系统 - 包含Docker部署配置和快速启动文档 - 添加自动登录功能和测试脚本
124 lines
2.9 KiB
Markdown
124 lines
2.9 KiB
Markdown
# Auto Login Feature
|
|
|
|
## Overview
|
|
|
|
The chatgpt-codex-router now automatically checks authentication status on startup and initiates OAuth login if no valid token is found.
|
|
|
|
## How It Works
|
|
|
|
1. **Startup Check**: When the server starts, it checks for an existing authentication token
|
|
2. **Token Validation**: If a token exists, it validates whether it's expired
|
|
3. **Auto Login**: If no token is found or the token is expired, the server automatically:
|
|
- Generates OAuth authorization flow
|
|
- Starts local OAuth callback server on port 1455
|
|
- Attempts to open browser automatically
|
|
- Displays OAuth URL in logs for manual access
|
|
|
|
## Startup Behavior
|
|
|
|
### ✅ Valid Token Found
|
|
|
|
```
|
|
[INFO] Authentication token found and valid.
|
|
[INFO] Server started on http://0.0.0.0:3000
|
|
```
|
|
|
|
### ⚠️ No Token Found
|
|
|
|
```
|
|
[WARN] No authentication token found. Initiating OAuth login...
|
|
[INFO] Starting OAuth flow with state: ...
|
|
[INFO] Local OAuth server started on port 1455
|
|
[INFO] OAuth login initiated.
|
|
[INFO] Please complete the OAuth flow in your browser.
|
|
[INFO] OAuth URL: https://auth.openai.com/oauth/authorize?...
|
|
[INFO] Browser should have opened automatically.
|
|
[INFO] Server started on http://0.0.0.0:3000
|
|
```
|
|
|
|
### ⚠️ Token Expired
|
|
|
|
```
|
|
[WARN] Authentication token expired. Please login again.
|
|
[INFO] Starting OAuth flow with state: ...
|
|
...
|
|
```
|
|
|
|
## Manual Login
|
|
|
|
If you want to manually trigger OAuth login after the server is running:
|
|
|
|
```bash
|
|
curl -X POST http://localhost:3000/auth/login
|
|
```
|
|
|
|
## Configuration
|
|
|
|
### Disable Auto Login
|
|
|
|
If you want to disable auto-login, you can:
|
|
1. Create a dummy token file (not recommended)
|
|
2. Modify the check logic in `src/index.ts`
|
|
|
|
### Custom OAuth Server Port
|
|
|
|
To use a different port for the OAuth callback server, modify your config:
|
|
|
|
```json
|
|
{
|
|
"oauth": {
|
|
"localServerPort": 1456
|
|
}
|
|
}
|
|
```
|
|
|
|
Or set via environment variable:
|
|
|
|
```bash
|
|
OAUTH_LOCAL_SERVER_PORT=1456 npm start
|
|
```
|
|
|
|
## Token Storage
|
|
|
|
Tokens are stored in:
|
|
- **Linux/Mac**: `~/.chatgpt-codex-router/tokens.json`
|
|
- **Windows**: `C:\Users\<username>\.chatgpt-codex-router\tokens.json`
|
|
|
|
Token file structure:
|
|
```json
|
|
{
|
|
"access_token": "...",
|
|
"refresh_token": "...",
|
|
"expires_at": 1234567890,
|
|
"account_id": "...",
|
|
"updated_at": 1234567890
|
|
}
|
|
```
|
|
|
|
## Troubleshooting
|
|
|
|
### Port 1455 Already in Use
|
|
|
|
If you see the error:
|
|
```
|
|
[WARN] OAuth server not ready, manual login required.
|
|
```
|
|
|
|
It means port 1455 is already in use. You can:
|
|
1. Kill the process using port 1455: `lsof -ti:1455 | xargs kill -9`
|
|
2. Use a different port via configuration
|
|
3. Manually login using: `curl -X POST http://localhost:3000/auth/login`
|
|
|
|
### Browser Not Opening
|
|
|
|
If the browser doesn't open automatically:
|
|
1. Copy the OAuth URL from the logs
|
|
2. Paste it in your browser manually
|
|
|
|
### OAuth Callback Failing
|
|
|
|
If OAuth callback fails:
|
|
1. Check that the OAuth callback server is running on port 1455
|
|
2. Verify firewall settings
|
|
3. Check logs in `logs/` directory for detailed error messages
|