-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathforgot-password.tsx
More file actions
189 lines (175 loc) · 5.07 KB
/
Copy pathforgot-password.tsx
File metadata and controls
189 lines (175 loc) · 5.07 KB
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 { type SEOHandle } from '@nasa-gcn/remix-seo'
import * as E from '@react-email/components'
import { data, redirect, Link, useFetcher } from 'react-router'
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 { type Route } from './+types/forgot-password.ts'
import { prepareVerification } from './verify.server.ts'
export const handle: SEOHandle = {
getSitemapEntries: () => null,
}
const ForgotPasswordSchema = z.object({
usernameOrEmail: z.union([EmailSchema, UsernameSchema]),
})
export async function action({ request }: Route.ActionArgs) {
const formData = await request.formData()
await 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 data(
{ 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 data(
{ 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: Route.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 pt-20 pb-32">
<div className="flex flex-col justify-center">
<div className="text-center">
<h1 className="text-h1">Forgot Password</h1>
<p className="text-body-md text-muted-foreground mt-3">
No worries, we'll send you reset instructions.
</p>
</div>
<div className="mx-auto mt-16 max-w-sm min-w-full 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="text-body-sm mt-11 text-center font-bold"
>
Back to Login
</Link>
</div>
</div>
</div>
)
}
export function ErrorBoundary() {
return <GeneralErrorBoundary />
}