PersonalWebApplication/Client/src/api/auth.ts
Ryan c61b8de776 重构登录后博客页:Lunada 渐变背景 + 两侧图片液态玻璃;登录报错改英文
- 登录页凭据/网络/HTTP 错误提示改为英文
- 博客页改为:通栏顶部彩虹栏 + 通栏 Hero(彩虹光标动效)+ 图片·内容·图片三栏
- 两侧图片用 import.meta.glob 自动索引 src/assets/sides/,按高度自适应、居中,叠加液态玻璃覆层
- 背景改为 Lunada 渐变极光
- 性能优化:极光改静止、中间纸/顶栏去掉 backdrop-filter,降低多层毛玻璃每帧重算开销

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-18 10:57:00 +08:00

39 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 登录接口封装。后端基址默认本机 8080可用 VITE_API_BASE 覆盖
// (与 useSystemStats 保持一致)。
const API_BASE =
(import.meta.env.VITE_API_BASE as string | undefined) ?? 'http://localhost:8080'
export interface LoginResult {
ok: boolean
username: string
token: string
}
/**
* 提交用户名/明文密码到后端校验。
* - 成功:解析 200 响应体(含 token
* - 凭据错误:后端返回 401这里抛出可读错误。
* - 后端不可达fetch 失败,抛出网络错误。
*/
export async function login(username: string, password: string): Promise<LoginResult> {
let res: Response
try {
res = await fetch(`${API_BASE}/api/web/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password }),
})
} catch {
throw new Error('Cannot reach the server. Please make sure the backend is running.')
}
if (res.status === 401) {
throw new Error('Incorrect username or password')
}
if (!res.ok) {
throw new Error(`Sign-in failed (HTTP ${res.status})`)
}
return (await res.json()) as LoginResult
}