-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.tsx
130 lines (122 loc) · 3.67 KB
/
page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use client'
import { authenticate } from '@/actions/auth'
import { Button } from '@/components/ui/button'
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { zodResolver } from '@hookform/resolvers/zod'
import { useRouter } from 'next/navigation'
import { useState } from 'react'
import { useForm } from 'react-hook-form'
import { toast } from 'sonner'
import { z } from 'zod'
const loginSchema = z.object({
email: z.string().email({ message: 'Invalid email address' }),
password: z
.string()
.min(6, { message: 'Password must be at least 6 characters' }),
})
export default function Auth() {
const form = useForm<z.infer<typeof loginSchema>>({
resolver: zodResolver(loginSchema),
defaultValues: {
email: '',
password: '',
},
})
const [isAuthenticating, setIsAuthenticating] = useState(false)
const router = useRouter()
const onSubmit = async ({ email, password }: z.infer<typeof loginSchema>) => {
setIsAuthenticating(true)
try {
const error = await authenticate(email, password)
// 返回值不为空,提示登录失败
if (error) {
toast.error('Invalid email or password', {
position: 'top-right',
})
} else {
// 登录成功,提示登录成功 并跳转到后台管理页面
toast.success('Logged in successfully', {
position: 'top-right',
})
router.push('/admin')
}
} catch (error) {
// 捕获到异常,提示异常中的错误信息
if (error instanceof Error) {
toast.error(error.message, {
position: 'top-right',
})
} else {
toast.error('An error occurred while authenticating', {
position: 'top-right',
})
}
} finally {
setIsAuthenticating(false)
}
}
return (
<div className="flex h-svh items-center justify-center">
<div className="mx-auto grid w-[350px] gap-6">
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="grid gap-4">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem className="grid gap-2">
<FormLabel htmlFor="email">Email</FormLabel>
<FormControl>
<Input
id="email"
type="email"
placeholder="[email protected]"
{...field}
disabled={isAuthenticating}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem className="grid gap-2">
<div className="flex items-center">
<FormLabel htmlFor="password">Password</FormLabel>
</div>
<FormControl>
<Input
disabled={isAuthenticating}
id="password"
type="password"
{...field}
/>
</FormControl>{' '}
<FormMessage />
</FormItem>
)}
/>
<Button
disabled={isAuthenticating}
type="submit"
className="w-full"
>
Login
</Button>
</form>
</Form>
</div>
</div>
)
}