Skip to content

Commit be43abf

Browse files
committed
feat: auto-detect token on login page via /api/setup-hint (localhost only)
1 parent 91b9deb commit be43abf

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

h5/src/i18n.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const messages = {
2626
'setup.tips.token.title': 'Token 填什么?',
2727
'setup.tips.token.desc': '安装 ClawApp 时你设置的连接密码。\n• 如果忘记了,可以在电脑上查看 server/.env 文件中的 PROXY_TOKEN\n• 也可以重新运行安装脚本重新设置',
2828
'setup.tips.doc': '查看完整文档',
29+
'setup.token.autofill': '✅ 已自动检测到连接密码,直接点连接即可',
2930
// 聊天页
3031
'chat.input.placeholder': '输入消息...',
3132
'chat.send': '发送',
@@ -175,6 +176,7 @@ const messages = {
175176
'setup.tips.token.title': 'What is Token?',
176177
'setup.tips.token.desc': 'The connection password you set during ClawApp installation.\n• If forgotten, check PROXY_TOKEN in server/.env on your PC\n• Or re-run the install script to reset it',
177178
'setup.tips.doc': 'Full Documentation',
179+
'setup.token.autofill': '✅ Token auto-detected, just tap Connect',
178180
'chat.input.placeholder': 'Type a message...',
179181
'chat.send': 'Send',
180182
'chat.abort': 'Stop',

h5/src/main.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function createSetupPage() {
5252
<label>${t('setup.token')}</label>
5353
<input type="password" id="input-token" placeholder="${t('setup.token.placeholder')}" />
5454
</div>
55+
<div class="setup-hint" id="setup-hint" style="display:none"></div>
5556
<button class="btn-primary" id="connect-btn">${t('setup.connect')}</button>
5657
<div class="setup-error" id="setup-error"></div>
5758
<div class="setup-tips">
@@ -103,6 +104,18 @@ function initApp() {
103104
tokenInput.value = config.token
104105
}
105106

107+
// 自动检测 Token(仅 localhost 可用)
108+
if (!config?.token) {
109+
fetch('/api/setup-hint').then(r => r.json()).then(data => {
110+
if (data.ok && data.token && !tokenInput.value) {
111+
tokenInput.value = data.token
112+
tokenInput.type = 'text'
113+
const hint = document.getElementById('setup-hint')
114+
if (hint) { hint.textContent = t('setup.token.autofill'); hint.style.display = '' }
115+
}
116+
}).catch(() => {})
117+
}
118+
106119
connectBtn.onclick = () => {
107120
const host = hostInput.value.trim()
108121
const token = tokenInput.value.trim()

h5/src/style.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ html, body {
218218
min-height: 20px;
219219
}
220220

221+
/* 自动检测 Token 提示 */
222+
.setup-hint {
223+
color: var(--success);
224+
font-size: 13px;
225+
text-align: center;
226+
margin-bottom: 4px;
227+
padding: 6px 0;
228+
}
229+
221230
/* 登录页帮助提示 */
222231
.setup-tips {
223232
margin-top: 16px;

server/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,19 @@ app.get('/media', (req, res) => {
760760

761761
// ==================== API 路由 ====================
762762

763+
/** GET /api/setup-hint — 仅 localhost 可访问,返回自动生成的连接密码 */
764+
app.get('/api/setup-hint', (req, res) => {
765+
const ip = req.ip || req.connection?.remoteAddress || '';
766+
const isLocal = ['127.0.0.1', '::1', '::ffff:127.0.0.1', 'localhost'].includes(ip);
767+
if (!isLocal) {
768+
return res.status(403).json({ ok: false });
769+
}
770+
if (!CONFIG.proxyToken) {
771+
return res.json({ ok: false });
772+
}
773+
res.json({ ok: true, token: CONFIG.proxyToken });
774+
});
775+
763776
/** POST /api/connect — 建立会话 */
764777
app.post('/api/connect', async (req, res) => {
765778
const { token } = req.body || {};

0 commit comments

Comments
 (0)