|
1 | | -'use client' |
| 1 | +import ForgotPassword from './forgot-password-form' |
2 | 2 |
|
3 | | -import { zodResolver } from '@hookform/resolvers/zod' |
4 | | -import { useHookFormAction } from '@next-safe-action/adapter-react-hook-form/hooks' |
5 | | -import { useSearchParams } from 'next/navigation' |
6 | | -import { useEffect, useState } from 'react' |
7 | | -import { AUTH_URLS } from '@/configs/urls' |
8 | | -import { |
9 | | - getTimeoutMsFromUserMessage, |
10 | | - USER_MESSAGES, |
11 | | -} from '@/configs/user-messages' |
12 | | -import { forgotPasswordAction } from '@/core/server/actions/auth-actions' |
13 | | -import { forgotPasswordSchema } from '@/core/server/functions/auth/auth.types' |
14 | | -import { AuthFormMessage, type AuthMessage } from '@/features/auth/form-message' |
15 | | -import { Button } from '@/ui/primitives/button' |
16 | | -import { Input } from '@/ui/primitives/input' |
17 | | -import { Label } from '@/ui/primitives/label' |
18 | | - |
19 | | -export default function ForgotPassword() { |
20 | | - const searchParams = useSearchParams() |
21 | | - const [message, setMessage] = useState<AuthMessage | undefined>() |
22 | | - |
23 | | - const { |
24 | | - form, |
25 | | - handleSubmitWithAction, |
26 | | - action: { isExecuting }, |
27 | | - } = useHookFormAction( |
28 | | - forgotPasswordAction, |
29 | | - zodResolver(forgotPasswordSchema), |
30 | | - { |
31 | | - actionProps: { |
32 | | - onSuccess: () => { |
33 | | - form.reset() |
34 | | - setMessage({ success: USER_MESSAGES.passwordReset.message }) |
35 | | - }, |
36 | | - onError: ({ error }) => { |
37 | | - if (error.serverError) { |
38 | | - setMessage({ error: error.serverError }) |
39 | | - } |
40 | | - }, |
41 | | - }, |
42 | | - } |
43 | | - ) |
44 | | - |
45 | | - useEffect(() => { |
46 | | - const email = searchParams.get('email') |
47 | | - if (email) { |
48 | | - form.setValue('email', email) |
49 | | - } |
50 | | - }, [searchParams, form]) |
51 | | - |
52 | | - useEffect(() => { |
53 | | - if ( |
54 | | - message && |
55 | | - (('success' in message && message.success) || |
56 | | - ('error' in message && message.error)) |
57 | | - ) { |
58 | | - const timer = setTimeout( |
59 | | - () => setMessage(undefined), |
60 | | - getTimeoutMsFromUserMessage( |
61 | | - 'success' in message |
62 | | - ? message.success! |
63 | | - : 'error' in message |
64 | | - ? message.error! |
65 | | - : '' |
66 | | - ) || 5000 |
67 | | - ) |
68 | | - return () => clearTimeout(timer) |
69 | | - } |
70 | | - }, [message]) |
71 | | - |
72 | | - const handleBackToSignIn = () => { |
73 | | - const email = form.getValues('email') |
74 | | - const searchParams = email ? `?email=${encodeURIComponent(email)}` : '' |
75 | | - window.location.href = `${AUTH_URLS.SIGN_IN}${searchParams}` |
76 | | - } |
77 | | - |
78 | | - return ( |
79 | | - <div className="flex w-full flex-col"> |
80 | | - <h1>Reset Password</h1> |
81 | | - <p className="text-fg-secondary leading-6"> |
82 | | - Remember your password?{' '} |
83 | | - <button |
84 | | - type="button" |
85 | | - onClick={handleBackToSignIn} |
86 | | - className="text-fg underline" |
87 | | - > |
88 | | - Sign in |
89 | | - </button> |
90 | | - . |
91 | | - </p> |
92 | | - |
93 | | - <form |
94 | | - onSubmit={handleSubmitWithAction} |
95 | | - className="mt-5 flex flex-col gap-2" |
96 | | - > |
97 | | - <Label htmlFor="email">E-Mail</Label> |
98 | | - <Input |
99 | | - {...form.register('email')} |
100 | | - id="email" |
101 | | - name="email" |
102 | | - type="email" |
103 | | - placeholder="you@example.com" |
104 | | - required |
105 | | - /> |
106 | | - <Button type="submit" loading={isExecuting ? 'Sending...' : undefined}> |
107 | | - Reset Password |
108 | | - </Button> |
109 | | - </form> |
110 | | - |
111 | | - {message && <AuthFormMessage className="mt-4" message={message} />} |
112 | | - </div> |
113 | | - ) |
| 3 | +export default function Page() { |
| 4 | + return <ForgotPassword /> |
114 | 5 | } |
0 commit comments