mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
feat: 优化 Gemini OAuth 授权流程,使用固定的 localhost 回调地址
- 将 Gemini OAuth 回调地址固定为 http://localhost:45462 - 更新前端提示文字为"复制oauth后的链接" - 实现自动提取 localhost:45462 链接中的 code 参数功能 - 删除不再需要的 web/auth_gemini 路由 - 添加详细的用户操作说明和错误处理提示 - 支持两种输入方式:完整链接或仅授权码 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -406,10 +406,8 @@ router.post('/gemini-accounts/generate-auth-url', authenticateAdmin, async (req,
|
||||
try {
|
||||
const { state } = req.body;
|
||||
|
||||
// 构建 redirect_uri,使用当前服务的地址
|
||||
const protocol = req.protocol;
|
||||
const host = req.get('host');
|
||||
const redirectUri = `${protocol}://${host}/web/auth_gemini`;
|
||||
// 使用固定的 localhost:45462 作为回调地址
|
||||
const redirectUri = 'http://localhost:45462';
|
||||
|
||||
logger.info(`Generating Gemini OAuth URL with redirect_uri: ${redirectUri}`);
|
||||
|
||||
@@ -420,7 +418,7 @@ router.post('/gemini-accounts/generate-auth-url', authenticateAdmin, async (req,
|
||||
await redis.setOAuthSession(sessionId, {
|
||||
state: authState,
|
||||
type: 'gemini',
|
||||
redirectUri, // 保存 redirect_uri 用于 token 交换
|
||||
redirectUri: redirectUri, // 保存固定的 redirect_uri 用于 token 交换
|
||||
createdAt: new Date().toISOString()
|
||||
});
|
||||
|
||||
@@ -470,15 +468,9 @@ router.post('/gemini-accounts/exchange-code', authenticateAdmin, async (req, res
|
||||
return res.status(400).json({ error: 'Authorization code is required' });
|
||||
}
|
||||
|
||||
// 如果提供了 sessionId,从会话中获取 redirect_uri
|
||||
let redirectUri = null;
|
||||
if (sessionId) {
|
||||
const oauthSession = await redis.getOAuthSession(sessionId);
|
||||
if (oauthSession && oauthSession.redirectUri) {
|
||||
redirectUri = oauthSession.redirectUri;
|
||||
logger.info(`Using redirect_uri from session: ${redirectUri}`);
|
||||
}
|
||||
}
|
||||
// 使用固定的 localhost:45462 作为 redirect_uri
|
||||
const redirectUri = 'http://localhost:45462';
|
||||
logger.info(`Using fixed redirect_uri: ${redirectUri}`);
|
||||
|
||||
const tokens = await geminiAccountService.exchangeCodeForTokens(code, redirectUri);
|
||||
|
||||
|
||||
@@ -371,174 +371,5 @@ router.get('/style.css', (req, res) => {
|
||||
});
|
||||
|
||||
// 🔑 Gemini OAuth 回调页面
|
||||
router.get('/auth_gemini', (req, res) => {
|
||||
try {
|
||||
const code = req.query.code || '';
|
||||
const state = req.query.state || '';
|
||||
const error = req.query.error || '';
|
||||
const errorDescription = req.query.error_description || '';
|
||||
|
||||
// 简单的 HTML 页面,用于显示授权码
|
||||
const html = `
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Gemini 授权回调</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background-color: #f5f5f5;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: 100vh;
|
||||
}
|
||||
.container {
|
||||
background: white;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
padding: 40px;
|
||||
max-width: 600px;
|
||||
width: 90%;
|
||||
}
|
||||
h1 {
|
||||
color: #333;
|
||||
margin-bottom: 20px;
|
||||
font-size: 24px;
|
||||
}
|
||||
.success {
|
||||
color: #22c55e;
|
||||
}
|
||||
.error {
|
||||
color: #ef4444;
|
||||
}
|
||||
.code-box {
|
||||
background: #f3f4f6;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 6px;
|
||||
padding: 15px;
|
||||
margin: 20px 0;
|
||||
word-break: break-all;
|
||||
font-family: 'Courier New', monospace;
|
||||
font-size: 14px;
|
||||
position: relative;
|
||||
}
|
||||
.copy-button {
|
||||
background: #3b82f6;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
padding: 10px 20px;
|
||||
font-size: 16px;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
.copy-button:hover {
|
||||
background: #2563eb;
|
||||
}
|
||||
.copy-button:active {
|
||||
background: #1d4ed8;
|
||||
}
|
||||
.instructions {
|
||||
color: #6b7280;
|
||||
margin-top: 20px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
.step {
|
||||
margin: 10px 0;
|
||||
padding-left: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
${error ? `
|
||||
<h1 class="error">授权失败</h1>
|
||||
<div class="error">
|
||||
<p><strong>错误:</strong> ${error}</p>
|
||||
${errorDescription ? `<p><strong>描述:</strong> ${errorDescription}</p>` : ''}
|
||||
</div>
|
||||
<div class="instructions">
|
||||
<p>请关闭此页面并返回管理界面重试。</p>
|
||||
</div>
|
||||
` : `
|
||||
<h1 class="success">授权成功</h1>
|
||||
<p>请复制下面的授权码:</p>
|
||||
<div class="code-box" id="codeBox">
|
||||
${code}
|
||||
</div>
|
||||
<button class="copy-button" onclick="copyCode()">复制授权码</button>
|
||||
|
||||
<div class="instructions">
|
||||
<p><strong>接下来的步骤:</strong></p>
|
||||
<div class="step">1. 点击上方按钮复制授权码</div>
|
||||
<div class="step">2. 返回到管理界面的创建账户页面</div>
|
||||
<div class="step">3. 将授权码粘贴到"授权码"输入框中</div>
|
||||
<div class="step">4. 点击"使用授权码创建账户"按钮完成创建</div>
|
||||
</div>
|
||||
`}
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function copyCode() {
|
||||
const code = document.getElementById('codeBox').innerText;
|
||||
navigator.clipboard.writeText(code).then(() => {
|
||||
const button = document.querySelector('.copy-button');
|
||||
const originalText = button.innerText;
|
||||
button.innerText = '已复制!';
|
||||
button.style.background = '#22c55e';
|
||||
|
||||
setTimeout(() => {
|
||||
button.innerText = originalText;
|
||||
button.style.background = '#3b82f6';
|
||||
}, 2000);
|
||||
}).catch(err => {
|
||||
// 降级方案
|
||||
const selection = window.getSelection();
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(document.getElementById('codeBox'));
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
|
||||
try {
|
||||
document.execCommand('copy');
|
||||
const button = document.querySelector('.copy-button');
|
||||
button.innerText = '已复制!';
|
||||
button.style.background = '#22c55e';
|
||||
} catch (e) {
|
||||
alert('复制失败,请手动选择并复制授权码');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 自动选中授权码文本
|
||||
window.onload = function() {
|
||||
const codeBox = document.getElementById('codeBox');
|
||||
if (codeBox && !${!!error}) {
|
||||
const selection = window.getSelection();
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(codeBox);
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
}
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
|
||||
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
||||
res.send(html);
|
||||
|
||||
logger.info(`📄 Served Gemini OAuth callback page: ${error ? 'error' : 'success'}`);
|
||||
} catch (error) {
|
||||
logger.error('❌ Error serving Gemini OAuth callback:', error);
|
||||
res.status(500).send('Internal server error');
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user