refactor(auth): align auth UI and mock auth API

This commit is contained in:
zetaloop
2026-02-28 07:26:05 +08:00
parent bce99c4c54
commit f5df00df4e
11 changed files with 337 additions and 56 deletions
+22 -11
View File
@@ -4,10 +4,12 @@ import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import { IconInput } from "@/components/ui/icon-input"
import { Label } from "@/components/ui/label"
import { getCurrentUserForLogin } from "@/lib/api"
import { login as loginApi } from "@/lib/api"
import { toApiError } from "@/lib/errors"
import { notifyInfo } from "@/lib/toast"
import { useAuthStore } from "@/store/auth"
import { standardSchemaResolver } from "@hookform/resolvers/standard-schema"
import { Eye, EyeOff, Lock, Phone } from "lucide-react"
import { Eye, EyeOff, Lock, User } from "lucide-react"
import Link from "next/link"
import { useRouter } from "next/navigation"
import { useState } from "react"
@@ -15,13 +17,13 @@ import { useForm } from "react-hook-form"
import { z } from "zod"
const loginSchema = z.object({
phone: z.string().min(11, "请输入正确的手机号"),
username: z.string().min(1, "请输入用户名"),
password: z.string().min(6, "密码至少6位"),
})
export default function LoginPage() {
const router = useRouter()
const { login } = useAuthStore()
const { login: storeLogin } = useAuthStore()
const [showPassword, setShowPassword] = useState(false)
const {
register,
@@ -31,10 +33,14 @@ export default function LoginPage() {
resolver: standardSchemaResolver(loginSchema),
})
const onSubmit = async (_data: z.infer<typeof loginSchema>) => {
await new Promise((r) => setTimeout(r, 500))
login(getCurrentUserForLogin(), ["consumer", "player", "owner"])
router.push("/")
const onSubmit = async (data: z.infer<typeof loginSchema>) => {
try {
const user = await loginApi({ username: data.username, password: data.password })
storeLogin(user, ["consumer", "player", "owner"])
router.push("/")
} catch (err) {
notifyInfo(toApiError(err).msg)
}
}
return (
@@ -46,9 +52,14 @@ export default function LoginPage() {
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
<div className="space-y-1.5">
<Label htmlFor="phone"></Label>
<IconInput id="phone" icon={<Phone />} placeholder="输入手机号" {...register("phone")} />
{errors.phone && <p className="text-xs text-destructive">{errors.phone.message}</p>}
<Label htmlFor="username"></Label>
<IconInput
id="username"
icon={<User />}
placeholder="输入用户名"
{...register("username")}
/>
{errors.username && <p className="text-xs text-destructive">{errors.username.message}</p>}
</div>
<div className="space-y-1.5">