feat(auth): connect to backend endpoints
This commit is contained in:
+16
-40
@@ -1,6 +1,6 @@
|
|||||||
import type { User } from "@/lib/types"
|
import type { User } from "@/lib/types"
|
||||||
|
|
||||||
import { mockUsers } from "@/lib/mock"
|
import { httpJson } from "./http"
|
||||||
|
|
||||||
export type RegisterInput = {
|
export type RegisterInput = {
|
||||||
username: string
|
username: string
|
||||||
@@ -18,42 +18,21 @@ function throwApiError(code: number, msg: string): never {
|
|||||||
throw { code, msg }
|
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> {
|
export async function register(input: RegisterInput): Promise<User> {
|
||||||
const username = input.username.trim()
|
const username = input.username.trim()
|
||||||
const email = input.email.trim()
|
const email = input.email.trim()
|
||||||
const password = input.password
|
const password = input.password
|
||||||
|
const vcode = input.vcode?.trim()
|
||||||
|
|
||||||
if (!username) throwApiError(400, "请输入用户名")
|
if (!username) throwApiError(400, "请输入用户名")
|
||||||
if (!email) throwApiError(400, "请输入邮箱")
|
if (!email) throwApiError(400, "请输入邮箱")
|
||||||
if (!password || password.length < 6) throwApiError(400, "密码至少6位")
|
if (!password || password.length < 6) throwApiError(400, "密码至少6位")
|
||||||
|
|
||||||
const usernameTaken = mockUsers.some(
|
const res = await httpJson<{ user: User }>("/api/v1/auth/register", {
|
||||||
(user) => user.username.toLowerCase() === username.toLowerCase(),
|
method: "POST",
|
||||||
)
|
json: { username, email, password, vcode },
|
||||||
if (usernameTaken) throwApiError(400, "用户名已存在")
|
})
|
||||||
|
return res.user
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function login(input: LoginInput): Promise<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 (!username) throwApiError(400, "请输入用户名")
|
||||||
if (!password || password.length < 6) throwApiError(400, "密码至少6位")
|
if (!password || password.length < 6) throwApiError(400, "密码至少6位")
|
||||||
|
|
||||||
await new Promise((r) => setTimeout(r, 500))
|
const res = await httpJson<{ user: User }>("/api/v1/auth/login", {
|
||||||
|
method: "POST",
|
||||||
const user =
|
json: { username, password },
|
||||||
mockUsers.find((u) => u.username.toLowerCase() === username.toLowerCase()) ??
|
})
|
||||||
mockUsers.find((u) => u.email?.toLowerCase() === username.toLowerCase())
|
return res.user
|
||||||
|
|
||||||
if (!user) throwApiError(404, "用户不存在")
|
|
||||||
return user
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function resetPassword(input: {
|
export async function resetPassword(input: {
|
||||||
@@ -86,8 +62,8 @@ export async function resetPassword(input: {
|
|||||||
if (!vcode) throwApiError(400, "请输入验证码")
|
if (!vcode) throwApiError(400, "请输入验证码")
|
||||||
if (!newPassword || newPassword.length < 8) throwApiError(400, "密码至少8位")
|
if (!newPassword || newPassword.length < 8) throwApiError(400, "密码至少8位")
|
||||||
|
|
||||||
await new Promise((r) => setTimeout(r, 500))
|
await httpJson<unknown>("/api/v1/auth/reset-password", {
|
||||||
|
method: "POST",
|
||||||
const user = mockUsers.find((u) => u.email?.toLowerCase() === email.toLowerCase())
|
json: { email, vcode, newPassword },
|
||||||
if (!user) throwApiError(404, "用户不存在")
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user