Files
sundynix-pets/pets-be/web/admin/src/pages/Login.tsx
T
Blizzard 9adafb1354 feat(auth): access token 缩到 2 小时 + refresh token 机制
后台登录框原来预填了 sundynix/sundynix,等于把凭证写在页面上,去掉。

令牌改为 access(2h) + refresh 两段式:
- 新表 sundynix_refresh_tokens,只存 sha256,明文只在签发时返回一次
- 有效期:小程序 30 天、后台 7 天
- 新接口 /api/auth/refresh|logout、/api/admin/refresh|logout

安全约定:
- 每次续期都轮换刷新令牌,旧的立即作废
- 作废后 60 秒内再到达算并发重试放行,超过则判定泄露、吊销该账号全部会话
- 主动退出与被连坐吊销的令牌不吃宽限期,否则「吊销全部」形同虚设
- 禁用用户时一并吊销刷新令牌,最多 2 小时彻底失去访问

两端请求层都做了单飞续期:并发请求同时 401 只发一次 refresh,
否则刷新令牌会被并发轮换掉互相打架。小程序续期失败回退 wx.login,
登录/续期请求标 noAuthRetry,避免登录失败(同样返回 40100)触发自我套娃。

顺带修掉 vite 代理仍指向 8080 的遗留(端口早已改 9090)。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-29 09:38:19 +08:00

74 lines
2.4 KiB
TypeScript

import { useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { api, setToken } from '@/lib/api'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
export default function Login() {
// 不预填账号密码:预填等于把后台凭证写在页面上
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState('')
const [loading, setLoading] = useState(false)
const navigate = useNavigate()
async function submit(e: React.FormEvent) {
e.preventDefault()
if (!username.trim() || !password) {
setError('请输入账号和密码')
return
}
setLoading(true)
setError('')
try {
const res = await api.login(username.trim(), password)
setToken(res.token, res.refresh_token)
navigate('/')
} catch (err: any) {
setError(err.message || '登录失败')
} finally {
setLoading(false)
}
}
return (
<div className="flex min-h-screen items-center justify-center p-4">
<Card className="w-full max-w-sm">
<CardHeader>
<div className="text-3xl text-center mb-2">🐾</div>
<CardTitle className="text-center text-xl"> · </CardTitle>
</CardHeader>
<CardContent>
<form onSubmit={submit} className="space-y-4">
<div className="space-y-1.5">
<Label></Label>
<Input
value={username}
autoComplete="username"
placeholder="请输入账号"
onChange={(e) => setUsername(e.target.value)}
/>
</div>
<div className="space-y-1.5">
<Label></Label>
<Input
type="password"
value={password}
autoComplete="current-password"
placeholder="请输入密码"
onChange={(e) => setPassword(e.target.value)}
/>
</div>
{error && <p className="text-sm text-destructive">{error}</p>}
<Button type="submit" className="w-full" disabled={loading}>
{loading ? '登录中...' : '登录'}
</Button>
</form>
</CardContent>
</Card>
</div>
)
}