|
1
|
import { zodResolver } from "@hookform/resolvers/zod" |
|
2
|
import { Alert, Button, Heading, Input, Text, toast } from "@medusajs/ui" |
|
3
|
import { useForm } from "react-hook-form" |
|
4
|
import { Trans, useTranslation } from "react-i18next" |
|
5
|
import { Link, useNavigate, useSearchParams } from "react-router-dom" |
|
6
|
import * as z from "zod" |
|
7
|
|
|
8
|
import { useState } from "react" |
|
9
|
import { decodeToken } from "react-jwt" |
|
10
|
import { Form } from "../../components/common/form" |
|
11
|
import { LogoBox } from "../../components/common/logo-box" |
|
12
|
import { i18n } from "../../components/utilities/i18n" |
|
13
|
import { |
|
14
|
useResetPasswordForEmailPass, |
|
15
|
useUpdateProviderForEmailPass, |
|
16
|
} from "../../hooks/api/auth" |
|
17
|
|
|
18
|
const ResetPasswordInstructionsSchema = z.object({ |
|
19
|
email: z.string().email(), |
|
20
|
}) |
|
21
|
|
|
22
|
const ResetPasswordSchema = z |
|
23
|
.object({ |
|
24
|
password: z.string().min(1), |
|
25
|
repeat_password: z.string().min(1), |
|
26
|
}) |
|
27
|
.superRefine(({ password, repeat_password }, ctx) => { |
|
28
|
if (password !== repeat_password) { |
|
29
|
ctx.addIssue({ |
|
30
|
code: z.ZodIssueCode.custom, |
|
31
|
message: i18n.t("resetPassword.passwordMismatch"), |
|
32
|
path: ["repeat_password"], |
|
33
|
}) |
|
34
|
} |
|
35
|
}) |
|
36
|
|
|
37
|
const ResetPasswordTokenSchema = z.object({ |
|
38
|
entity_id: z.string(), |
|
39
|
provider: z.string(), |
|
40
|
exp: z.number(), |
|
41
|
iat: z.number(), |
|
42
|
}) |
|
43
|
|
|
44
|
type DecodedResetPasswordToken = { |
|
45
|
entity_id: string // -> email in here |
|
46
|
provider: string |
|
47
|
exp: string |
|
48
|
iat: string |
|
49
|
} |
|
50
|
|
|
51
|
const validateDecodedResetPasswordToken = ( |
|
52
|
decoded: any |
|
53
|
): decoded is DecodedResetPasswordToken => { |
|
54
|
return ResetPasswordTokenSchema.safeParse(decoded).success |
|
55
|
} |
|
56
|
|
|
57
|
const InvalidResetToken = () => { |
|
58
|
const { t } = useTranslation() |
|
59
|
const navigate = useNavigate() |
|
60
|
|
|
61
|
return ( |
|
62
|
<div className="bg-ui-bg-base flex min-h-dvh w-dvw items-center justify-center"> |
|
63
|
<div className="m-4 flex w-full max-w-[300px] flex-col items-center"> |
|
64
|
<LogoBox className="mb-4" /> |
|
65
|
<div className="mb-6 flex flex-col items-center"> |
|
66
|
<Heading>{t("resetPassword.invalidLinkTitle")}</Heading> |
|
67
|
<Text size="small" className="text-ui-fg-subtle text-center"> |
|
68
|
{t("resetPassword.invalidLinkHint")} |
|
69
|
</Text> |
|
70
|
</div> |
|
71
|
<div className="flex w-full flex-col gap-y-3"> |
|
72
|
<Button |
|
73
|
onClick={() => navigate("/reset-password", { replace: true })} |
|
74
|
className="w-full" |
|
75
|
type="submit" |
|
76
|
> |
|
77
|
{t("resetPassword.goToResetPassword")} |
|
78
|
</Button> |
|
79
|
</div> |
|
80
|
<span className="txt-small my-6"> |
|
81
|
<Trans |
|
82
|
i18nKey="resetPassword.backToLogin" |
|
83
|
components={[ |
|
84
|
<Link |
|
85
|
key="login-link" |
|
86
|
to="/login" |
|
87
|
className="text-ui-fg-interactive transition-fg hover:text-ui-fg-interactive-hover focus-visible:text-ui-fg-interactive-hover outline-none" |
|
88
|
/>, |
|
89
|
]} |
|
90
|
/> |
|
91
|
</span> |
|
92
|
</div> |
|
93
|
</div> |
|
94
|
) |
|
95
|
} |
|
96
|
|
|
97
|
const ChooseNewPassword = ({ token }: { token: string }) => { |
|
98
|
const { t } = useTranslation() |
|
99
|
|
|
100
|
const [showAlert, setShowAlert] = useState(false) |
|
101
|
|
|
102
|
const invite: DecodedResetPasswordToken | null = token |
|
103
|
? decodeToken(token) |
|
104
|
: null |
|
105
|
|
|
106
|
const isValidResetPasswordToken = |
|
107
|
invite && validateDecodedResetPasswordToken(invite) |
|
108
|
|
|
109
|
const form = useForm<z.infer<typeof ResetPasswordSchema>>({ |
|
110
|
resolver: zodResolver(ResetPasswordSchema), |
|
111
|
defaultValues: { |
|
112
|
password: "", |
|
113
|
repeat_password: "", |
|
114
|
}, |
|
115
|
}) |
|
116
|
|
|
117
|
const { mutateAsync, isPending } = useUpdateProviderForEmailPass(token) |
|
118
|
|
|
119
|
const handleSubmit = form.handleSubmit(async ({ password }) => { |
|
120
|
if (!invite) { |
|
121
|
return |
|
122
|
} |
|
123
|
|
|
124
|
await mutateAsync( |
|
125
|
{ |
|
126
|
password, |
|
127
|
}, |
|
128
|
{ |
|
129
|
onSuccess: () => { |
|
130
|
form.setValue("password", "") |
|
131
|
form.setValue("repeat_password", "") |
|
132
|
setShowAlert(true) |
|
133
|
}, |
|
134
|
onError: (error) => { |
|
135
|
toast.error(error.message) |
|
136
|
}, |
|
137
|
} |
|
138
|
) |
|
139
|
}) |
|
140
|
|
|
141
|
if (!isValidResetPasswordToken) { |
|
142
|
return <InvalidResetToken /> |
|
143
|
} |
|
144
|
|
|
145
|
return ( |
|
146
|
<div className="bg-ui-bg-subtle flex min-h-dvh w-dvw items-center justify-center"> |
|
147
|
<div className="m-4 flex w-full max-w-[280px] flex-col items-center"> |
|
148
|
<LogoBox className="mb-4" /> |
|
149
|
<div className="mb-6 flex flex-col items-center"> |
|
150
|
<Heading>{t("resetPassword.resetPassword")}</Heading> |
|
151
|
<Text size="small" className="text-ui-fg-subtle text-center"> |
|
152
|
{t("resetPassword.newPasswordHint")} |
|
153
|
</Text> |
|
154
|
</div> |
|
155
|
<div className="flex w-full flex-col gap-y-3"> |
|
156
|
<Form {...form}> |
|
157
|
<form |
|
158
|
onSubmit={handleSubmit} |
|
159
|
className="flex w-full flex-col gap-y-6" |
|
160
|
> |
|
161
|
<div className="flex flex-col gap-y-4"> |
|
162
|
<Input type="email" disabled value={invite?.entity_id} /> |
|
163
|
<Form.Field |
|
164
|
control={form.control} |
|
165
|
name="password" |
|
166
|
render={({ field }) => { |
|
167
|
return ( |
|
168
|
<Form.Item> |
|
169
|
<Form.Control> |
|
170
|
<Input |
|
171
|
autoComplete="new-password" |
|
172
|
type="password" |
|
173
|
{...field} |
|
174
|
placeholder={t("resetPassword.newPassword")} |
|
175
|
/> |
|
176
|
</Form.Control> |
|
177
|
<Form.ErrorMessage /> |
|
178
|
</Form.Item> |
|
179
|
) |
|
180
|
}} |
|
181
|
/> |
|
182
|
<Form.Field |
|
183
|
control={form.control} |
|
184
|
name="repeat_password" |
|
185
|
render={({ field }) => { |
|
186
|
return ( |
|
187
|
<Form.Item> |
|
188
|
<Form.Control> |
|
189
|
<Input |
|
190
|
autoComplete="off" |
|
191
|
type="password" |
|
192
|
{...field} |
|
193
|
placeholder={t("resetPassword.repeatNewPassword")} |
|
194
|
/> |
|
195
|
</Form.Control> |
|
196
|
<Form.ErrorMessage /> |
|
197
|
</Form.Item> |
|
198
|
) |
|
199
|
}} |
|
200
|
/> |
|
201
|
</div> |
|
202
|
{showAlert && ( |
|
203
|
<Alert dismissible variant="success"> |
|
204
|
<div className="flex flex-col"> |
|
205
|
<span className="text-ui-fg-base mb-1"> |
|
206
|
{t("resetPassword.successfulResetTitle")} |
|
207
|
</span> |
|
208
|
<span>{t("resetPassword.successfulReset")}</span> |
|
209
|
</div> |
|
210
|
</Alert> |
|
211
|
)} |
|
212
|
{!showAlert && ( |
|
213
|
<Button className="w-full" type="submit" isLoading={isPending}> |
|
214
|
{t("resetPassword.resetPassword")} |
|
215
|
</Button> |
|
216
|
)} |
|
217
|
</form> |
|
218
|
</Form> |
|
219
|
</div> |
|
220
|
<span className="txt-small my-6"> |
|
221
|
<Trans |
|
222
|
i18nKey="resetPassword.backToLogin" |
|
223
|
components={[ |
|
224
|
<Link |
|
225
|
key="login-link" |
|
226
|
to="/login" |
|
227
|
className="text-ui-fg-base transition-fg hover:text-ui-fg-base-hover focus-visible:text-ui-fg-base-hover outline-none" |
|
228
|
/>, |
|
229
|
]} |
|
230
|
/> |
|
231
|
</span> |
|
232
|
</div> |
|
233
|
</div> |
|
234
|
) |
|
235
|
} |
|
236
|
|
|
237
|
export const ResetPassword = () => { |
|
238
|
const { t } = useTranslation() |
|
239
|
const [searchParams] = useSearchParams() |
|
240
|
const [showAlert, setShowAlert] = useState(false) |
|
241
|
|
|
242
|
const token = searchParams.get("token") |
|
243
|
|
|
244
|
const form = useForm<z.infer<typeof ResetPasswordInstructionsSchema>>({ |
|
245
|
resolver: zodResolver(ResetPasswordInstructionsSchema), |
|
246
|
defaultValues: { |
|
247
|
email: "", |
|
248
|
}, |
|
249
|
}) |
|
250
|
|
|
251
|
const { mutateAsync, isPending } = useResetPasswordForEmailPass() |
|
252
|
|
|
253
|
const handleSubmit = form.handleSubmit(async ({ email }) => { |
|
254
|
await mutateAsync( |
|
255
|
{ |
|
256
|
email, |
|
257
|
}, |
|
258
|
{ |
|
259
|
onSuccess: () => { |
|
260
|
form.setValue("email", "") |
|
261
|
setShowAlert(true) |
|
262
|
}, |
|
263
|
onError: (error) => { |
|
264
|
toast.error(error.message) |
|
265
|
}, |
|
266
|
} |
|
267
|
) |
|
268
|
}) |
|
269
|
|
|
270
|
if (token) { |
|
271
|
return <ChooseNewPassword token={token} /> |
|
272
|
} |
|
273
|
|
|
274
|
return ( |
|
275
|
<div className="bg-ui-bg-base flex min-h-dvh w-dvw items-center justify-center"> |
|
276
|
<div className="m-4 flex w-full max-w-[300px] flex-col items-center"> |
|
277
|
<LogoBox className="mb-4" /> |
|
278
|
<div className="mb-4 flex flex-col items-center"> |
|
279
|
<Heading>{t("resetPassword.resetPassword")}</Heading> |
|
280
|
<Text size="small" className="text-ui-fg-subtle text-center"> |
|
281
|
{t("resetPassword.hint")} |
|
282
|
</Text> |
|
283
|
</div> |
|
284
|
<div className="flex w-full flex-col gap-y-3"> |
|
285
|
<Form {...form}> |
|
286
|
<form |
|
287
|
onSubmit={handleSubmit} |
|
288
|
className="flex w-full flex-col gap-y-6" |
|
289
|
> |
|
290
|
<div className="mt-4 flex flex-col gap-y-3"> |
|
291
|
<Form.Field |
|
292
|
control={form.control} |
|
293
|
name="email" |
|
294
|
render={({ field }) => { |
|
295
|
return ( |
|
296
|
<Form.Item> |
|
297
|
<Form.Control> |
|
298
|
<Input |
|
299
|
autoComplete="email" |
|
300
|
{...field} |
|
301
|
placeholder={t("fields.email")} |
|
302
|
/> |
|
303
|
</Form.Control> |
|
304
|
<Form.ErrorMessage /> |
|
305
|
</Form.Item> |
|
306
|
) |
|
307
|
}} |
|
308
|
/> |
|
309
|
</div> |
|
310
|
{showAlert && ( |
|
311
|
<Alert dismissible variant="success"> |
|
312
|
<div className="flex flex-col"> |
|
313
|
<span className="text-ui-fg-base mb-1"> |
|
314
|
{t("resetPassword.successfulRequestTitle")} |
|
315
|
</span> |
|
316
|
<span>{t("resetPassword.successfulRequest")}</span> |
|
317
|
</div> |
|
318
|
</Alert> |
|
319
|
)} |
|
320
|
<Button className="w-full" type="submit" isLoading={isPending}> |
|
321
|
{t("resetPassword.sendResetInstructions")} |
|
322
|
</Button> |
|
323
|
</form> |
|
324
|
</Form> |
|
325
|
</div> |
|
326
|
<span className="txt-small my-6"> |
|
327
|
<Trans |
|
328
|
i18nKey="resetPassword.backToLogin" |
|
329
|
components={[ |
|
330
|
<Link |
|
331
|
key="login-link" |
|
332
|
to="/login" |
|
333
|
className="text-ui-fg-base transition-fg hover:text-ui-fg-base-hover focus-visible:text-ui-fg-base-hover outline-none" |
|
334
|
/>, |
|
335
|
]} |
|
336
|
/> |
|
337
|
</span> |
|
338
|
</div> |
|
339
|
</div> |
|
340
|
) |
|
341
|
} |