-
Notifications
You must be signed in to change notification settings - Fork 417
/
Copy pathforgot-password.tsx
189 lines (176 loc) · 4.97 KB
/
forgot-password.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { getFormProps, getInputProps, useForm } from '@conform-to/react'
import { getZodConstraint, parseWithZod } from '@conform-to/zod'
import * as E from '@react-email/components'
import {
json,
redirect,
type ActionFunctionArgs,
type MetaFunction,
} from '@remix-run/node'
import { Link, useFetcher } from '@remix-run/react'
import { HoneypotInputs } from 'remix-utils/honeypot/react'
import { z } from 'zod'
import { GeneralErrorBoundary } from '#app/components/error-boundary.tsx'
import { ErrorList, Field } from '#app/components/forms.tsx'
import { StatusButton } from '#app/components/ui/status-button.tsx'
import { prisma } from '#app/utils/db.server.ts'
import { sendEmail } from '#app/utils/email.server.ts'
import { checkHoneypot } from '#app/utils/honeypot.server.ts'
import { EmailSchema, UsernameSchema } from '#app/utils/user-validation.ts'
import { prepareVerification } from './verify.server.ts'
const ForgotPasswordSchema = z.object({
usernameOrEmail: z.union([EmailSchema, UsernameSchema]),
})
export async function action({ request }: ActionFunctionArgs) {
const formData = await request.formData()
checkHoneypot(formData)
const submission = await parseWithZod(formData, {
schema: ForgotPasswordSchema.superRefine(async (data, ctx) => {
const user = await prisma.user.findFirst({
where: {
OR: [
{ email: data.usernameOrEmail },
{ username: data.usernameOrEmail },
],
},
select: { id: true },
})
if (!user) {
ctx.addIssue({
path: ['usernameOrEmail'],
code: z.ZodIssueCode.custom,
message: 'No user exists with this username or email',
})
return
}
}),
async: true,
})
if (submission.status !== 'success') {
return json(
{ result: submission.reply() },
{ status: submission.status === 'error' ? 400 : 200 },
)
}
const { usernameOrEmail } = submission.value
const user = await prisma.user.findFirstOrThrow({
where: { OR: [{ email: usernameOrEmail }, { username: usernameOrEmail }] },
select: { email: true, username: true },
})
const { verifyUrl, redirectTo, otp } = await prepareVerification({
period: 10 * 60,
request,
type: 'reset-password',
target: usernameOrEmail,
})
const response = await sendEmail({
to: user.email,
subject: `Epic Notes Password Reset`,
react: (
<ForgotPasswordEmail onboardingUrl={verifyUrl.toString()} otp={otp} />
),
})
if (response.status === 'success') {
return redirect(redirectTo.toString())
} else {
return json(
{ result: submission.reply({ formErrors: [response.error.message] }) },
{ status: 500 },
)
}
}
function ForgotPasswordEmail({
onboardingUrl,
otp,
}: {
onboardingUrl: string
otp: string
}) {
return (
<E.Html lang="en" dir="ltr">
<E.Container>
<h1>
<E.Text>Epic Notes Password Reset</E.Text>
</h1>
<p>
<E.Text>
Here's your verification code: <strong>{otp}</strong>
</E.Text>
</p>
<p>
<E.Text>Or click the link:</E.Text>
</p>
<E.Link href={onboardingUrl}>{onboardingUrl}</E.Link>
</E.Container>
</E.Html>
)
}
export const meta: MetaFunction = () => {
return [{ title: 'Password Recovery for Epic Notes' }]
}
export default function ForgotPasswordRoute() {
const forgotPassword = useFetcher<typeof action>()
const [form, fields] = useForm({
id: 'forgot-password-form',
constraint: getZodConstraint(ForgotPasswordSchema),
lastResult: forgotPassword.data?.result,
onValidate({ formData }) {
return parseWithZod(formData, { schema: ForgotPasswordSchema })
},
shouldRevalidate: 'onBlur',
})
return (
<div className="container pb-32 pt-20">
<div className="flex flex-col justify-center">
<div className="text-center">
<h1 className="text-h1">Forgot Password</h1>
<p className="mt-3 text-body-md text-muted-foreground">
No worries, we'll send you reset instructions.
</p>
</div>
<div className="mx-auto mt-16 min-w-full max-w-sm sm:min-w-[368px]">
<forgotPassword.Form method="POST" {...getFormProps(form)}>
<HoneypotInputs />
<div>
<Field
labelProps={{
htmlFor: fields.usernameOrEmail.id,
children: 'Username or Email',
}}
inputProps={{
autoFocus: true,
...getInputProps(fields.usernameOrEmail, { type: 'text' }),
}}
errors={fields.usernameOrEmail.errors}
/>
</div>
<ErrorList errors={form.errors} id={form.errorId} />
<div className="mt-6">
<StatusButton
className="w-full"
status={
forgotPassword.state === 'submitting'
? 'pending'
: form.status ?? 'idle'
}
type="submit"
disabled={forgotPassword.state !== 'idle'}
>
Recover password
</StatusButton>
</div>
</forgotPassword.Form>
<Link
to="/login"
className="mt-11 text-center text-body-sm font-bold"
>
Back to Login
</Link>
</div>
</div>
</div>
)
}
export function ErrorBoundary() {
return <GeneralErrorBoundary />
}