feat(frontend): implement center auth session flow with route guard

This commit is contained in:
root
2026-05-12 15:09:06 +00:00
parent 6219fbbcfe
commit d718128f89
12 changed files with 269 additions and 4 deletions

View File

@@ -1,8 +1,41 @@
import { useState } from 'react'
import type { FormEvent } from 'react'
import { useNavigate } from 'react-router-dom'
import { useAuth } from '../auth/auth-context'
export default function LoginPage() {
const navigate = useNavigate()
const { login, isAuthed, session } = useAuth()
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState('')
async function onSubmit(e: FormEvent) {
e.preventDefault()
setError('')
try {
await login(email, password)
navigate('/workspace')
} catch {
setError('登录失败,请检查账号密码')
}
}
return (
<section>
<h2></h2>
<p> Center token </p>
{isAuthed ? <p>{session?.user.email}</p> : null}
<form onSubmit={onSubmit} style={{ display: 'grid', gap: 8, maxWidth: 420 }}>
<input value={email} onChange={(e) => setEmail(e.target.value)} placeholder="Email" type="email" />
<input
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
type="password"
/>
<button type="submit"></button>
</form>
<p>{error}</p>
</section>
)
}

View File

@@ -33,6 +33,11 @@ export default function WorkspacePage() {
<section>
<h2></h2>
<form onSubmit={onSave} style={{ display: 'grid', gap: 8, maxWidth: 760 }}>
<input
value={form.centerApiBase}
onChange={(e) => setForm((v) => ({ ...v, centerApiBase: e.target.value }))}
placeholder="Center API Base"
/>
<input
value={form.guildApiBase}
onChange={(e) => setForm((v) => ({ ...v, guildApiBase: e.target.value }))}