feat(auth): connect to backend endpoints
This commit is contained in:
+16
-40
@@ -1,6 +1,6 @@
|
||||
import type { User } from "@/lib/types"
|
||||
|
||||
import { mockUsers } from "@/lib/mock"
|
||||
import { httpJson } from "./http"
|
||||
|
||||
export type RegisterInput = {
|
||||
username: string
|
||||
@@ -18,42 +18,21 @@ function throwApiError(code: number, msg: string): never {
|
||||
throw { code, msg }
|
||||
}
|
||||
|
||||
function nextUserId(): string {
|
||||
const maxId = Math.max(0, ...mockUsers.map((user) => Number.parseInt(user.id, 10) || 0))
|
||||
return String(maxId + 1)
|
||||
}
|
||||
|
||||
export async function register(input: RegisterInput): Promise<User> {
|
||||
const username = input.username.trim()
|
||||
const email = input.email.trim()
|
||||
const password = input.password
|
||||
const vcode = input.vcode?.trim()
|
||||
|
||||
if (!username) throwApiError(400, "请输入用户名")
|
||||
if (!email) throwApiError(400, "请输入邮箱")
|
||||
if (!password || password.length < 6) throwApiError(400, "密码至少6位")
|
||||
|
||||
const usernameTaken = mockUsers.some(
|
||||
(user) => user.username.toLowerCase() === username.toLowerCase(),
|
||||
)
|
||||
if (usernameTaken) throwApiError(400, "用户名已存在")
|
||||
|
||||
const emailTaken = mockUsers.some((user) => user.email?.toLowerCase() === email.toLowerCase())
|
||||
if (emailTaken) throwApiError(400, "邮箱已被注册")
|
||||
|
||||
await new Promise((r) => setTimeout(r, 500))
|
||||
|
||||
const user: User = {
|
||||
id: nextUserId(),
|
||||
username,
|
||||
email,
|
||||
nickname: username,
|
||||
avatar: "/avatars/u1.jpg",
|
||||
role: "consumer",
|
||||
createdAt: new Date().toISOString(),
|
||||
}
|
||||
|
||||
mockUsers.push(user)
|
||||
return user
|
||||
const res = await httpJson<{ user: User }>("/api/v1/auth/register", {
|
||||
method: "POST",
|
||||
json: { username, email, password, vcode },
|
||||
})
|
||||
return res.user
|
||||
}
|
||||
|
||||
export async function login(input: LoginInput): Promise<User> {
|
||||
@@ -63,14 +42,11 @@ export async function login(input: LoginInput): Promise<User> {
|
||||
if (!username) throwApiError(400, "请输入用户名")
|
||||
if (!password || password.length < 6) throwApiError(400, "密码至少6位")
|
||||
|
||||
await new Promise((r) => setTimeout(r, 500))
|
||||
|
||||
const user =
|
||||
mockUsers.find((u) => u.username.toLowerCase() === username.toLowerCase()) ??
|
||||
mockUsers.find((u) => u.email?.toLowerCase() === username.toLowerCase())
|
||||
|
||||
if (!user) throwApiError(404, "用户不存在")
|
||||
return user
|
||||
const res = await httpJson<{ user: User }>("/api/v1/auth/login", {
|
||||
method: "POST",
|
||||
json: { username, password },
|
||||
})
|
||||
return res.user
|
||||
}
|
||||
|
||||
export async function resetPassword(input: {
|
||||
@@ -86,8 +62,8 @@ export async function resetPassword(input: {
|
||||
if (!vcode) throwApiError(400, "请输入验证码")
|
||||
if (!newPassword || newPassword.length < 8) throwApiError(400, "密码至少8位")
|
||||
|
||||
await new Promise((r) => setTimeout(r, 500))
|
||||
|
||||
const user = mockUsers.find((u) => u.email?.toLowerCase() === email.toLowerCase())
|
||||
if (!user) throwApiError(404, "用户不存在")
|
||||
await httpJson<unknown>("/api/v1/auth/reset-password", {
|
||||
method: "POST",
|
||||
json: { email, vcode, newPassword },
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user