|
1
|
import { zodResolver } from "@hookform/resolvers/zod" |
|
2
|
import { Alert, Button, Heading, Hint, Input, Text, toast } from "@medusajs/ui" |
|
3
|
import i18n from "i18next" |
|
4
|
import { AnimatePresence, motion } from "motion/react" |
|
5
|
import { useState } from "react" |
|
6
|
import { useForm } from "react-hook-form" |
|
7
|
import { useTranslation } from "react-i18next" |
|
8
|
import { decodeToken } from "react-jwt" |
|
9
|
import { Link, useSearchParams } from "react-router-dom" |
|
10
|
import * as z from "zod" |
|
11
|
import { Form } from "../../components/common/form" |
|
12
|
import AvatarBox from "../../components/common/logo-box/avatar-box" |
|
13
|
import { |
|
14
|
useSignInWithEmailPass, |
|
15
|
useSignUpWithEmailPass, |
|
16
|
} from "../../hooks/api/auth" |
|
17
|
import { useAcceptInvite } from "../../hooks/api/invites" |
|
18
|
import { isFetchError } from "../../lib/is-fetch-error" |
|
19
|
import { AuthLoginResponse } from "@medusajs/js-sdk" |
|
20
|
|
|
21
|
const CreateAccountSchema = z |
|
22
|
.object({ |
|
23
|
email: z.string().email(), |
|
24
|
first_name: z.string().min(1), |
|
25
|
last_name: z.string().min(1), |
|
26
|
password: z.string().min(1), |
|
27
|
repeat_password: z.string().min(1), |
|
28
|
}) |
|
29
|
.superRefine(({ password, repeat_password }, ctx) => { |
|
30
|
if (password !== repeat_password) { |
|
31
|
ctx.addIssue({ |
|
32
|
code: z.ZodIssueCode.custom, |
|
33
|
message: i18n.t("invite.passwordMismatch"), |
|
34
|
path: ["repeat_password"], |
|
35
|
}) |
|
36
|
} |
|
37
|
}) |
|
38
|
|
|
39
|
// TODO: Update to V2 format |
|
40
|
type DecodedInvite = { |
|
41
|
id: string |
|
42
|
jti: any |
|
43
|
exp: string |
|
44
|
iat: number |
|
45
|
email: string |
|
46
|
} |
|
47
|
|
|
48
|
export const Invite = () => { |
|
49
|
const [searchParams] = useSearchParams() |
|
50
|
const [success, setSuccess] = useState(false) |
|
51
|
|
|
52
|
const token = searchParams.get("token") |
|
53
|
const invite: DecodedInvite | null = token ? decodeToken(token) : null |
|
54
|
const isValidInvite = invite && validateDecodedInvite(invite) |
|
55
|
|
|
56
|
return ( |
|
57
|
<div className="bg-ui-bg-subtle relative flex min-h-dvh w-dvw items-center justify-center p-4"> |
|
58
|
<div className="flex w-full max-w-[360px] flex-col items-center"> |
|
59
|
<AvatarBox checked={success} /> |
|
60
|
<div className="max-h-[557px] w-full will-change-contents"> |
|
61
|
{isValidInvite ? ( |
|
62
|
<AnimatePresence> |
|
63
|
{!success ? ( |
|
64
|
<motion.div |
|
65
|
key="create-account" |
|
66
|
initial={false} |
|
67
|
animate={{ |
|
68
|
height: "557px", |
|
69
|
y: 0, |
|
70
|
}} |
|
71
|
exit={{ |
|
72
|
height: 0, |
|
73
|
y: 40, |
|
74
|
}} |
|
75
|
transition={{ |
|
76
|
duration: 0.8, |
|
77
|
delay: 0.6, |
|
78
|
ease: [0, 0.71, 0.2, 1.01], |
|
79
|
}} |
|
80
|
className="w-full will-change-transform" |
|
81
|
> |
|
82
|
<motion.div |
|
83
|
initial={false} |
|
84
|
animate={{ |
|
85
|
opacity: 1, |
|
86
|
scale: 1, |
|
87
|
}} |
|
88
|
exit={{ |
|
89
|
opacity: 0, |
|
90
|
scale: 0.7, |
|
91
|
}} |
|
92
|
transition={{ |
|
93
|
duration: 0.6, |
|
94
|
delay: 0, |
|
95
|
ease: [0, 0.71, 0.2, 1.01], |
|
96
|
}} |
|
97
|
key="inner-create-account" |
|
98
|
> |
|
99
|
<CreateView |
|
100
|
onSuccess={() => setSuccess(true)} |
|
101
|
token={token!} |
|
102
|
invite={invite} |
|
103
|
/> |
|
104
|
</motion.div> |
|
105
|
</motion.div> |
|
106
|
) : ( |
|
107
|
<motion.div |
|
108
|
key="success-view" |
|
109
|
initial={{ |
|
110
|
opacity: 0, |
|
111
|
scale: 0.4, |
|
112
|
}} |
|
113
|
animate={{ |
|
114
|
opacity: 1, |
|
115
|
scale: 1, |
|
116
|
}} |
|
117
|
transition={{ |
|
118
|
duration: 1, |
|
119
|
delay: 0.6, |
|
120
|
ease: [0, 0.71, 0.2, 1.01], |
|
121
|
}} |
|
122
|
className="w-full" |
|
123
|
> |
|
124
|
<SuccessView /> |
|
125
|
</motion.div> |
|
126
|
)} |
|
127
|
</AnimatePresence> |
|
128
|
) : ( |
|
129
|
<InvalidView /> |
|
130
|
)} |
|
131
|
</div> |
|
132
|
</div> |
|
133
|
</div> |
|
134
|
) |
|
135
|
} |
|
136
|
|
|
137
|
const LoginLink = () => { |
|
138
|
const { t } = useTranslation() |
|
139
|
|
|
140
|
return ( |
|
141
|
<div className="flex w-full flex-col items-center"> |
|
142
|
<div className="my-6 h-px w-full border-b border-dotted" /> |
|
143
|
<Link |
|
144
|
key="login-link" |
|
145
|
to="/login" |
|
146
|
className="txt-small text-ui-fg-base transition-fg hover:text-ui-fg-base-hover focus-visible:text-ui-fg-base-hover font-medium outline-none" |
|
147
|
> |
|
148
|
{t("invite.backToLogin")} |
|
149
|
</Link> |
|
150
|
</div> |
|
151
|
) |
|
152
|
} |
|
153
|
|
|
154
|
const InvalidView = () => { |
|
155
|
const { t } = useTranslation() |
|
156
|
|
|
157
|
return ( |
|
158
|
<div className="flex flex-col items-center"> |
|
159
|
<div className="flex flex-col items-center gap-y-1"> |
|
160
|
<Heading>{t("invite.invalidTokenTitle")}</Heading> |
|
161
|
<Text size="small" className="text-ui-fg-subtle text-center"> |
|
162
|
{t("invite.invalidTokenHint")} |
|
163
|
</Text> |
|
164
|
</div> |
|
165
|
<LoginLink /> |
|
166
|
</div> |
|
167
|
) |
|
168
|
} |
|
169
|
|
|
170
|
const CreateView = ({ |
|
171
|
onSuccess, |
|
172
|
token, |
|
173
|
invite, |
|
174
|
}: { |
|
175
|
onSuccess: () => void |
|
176
|
token: string |
|
177
|
invite: DecodedInvite |
|
178
|
}) => { |
|
179
|
const { t } = useTranslation() |
|
180
|
const [invalid, setInvalid] = useState(false) |
|
181
|
|
|
182
|
const [params] = useSearchParams() |
|
183
|
const isFirstRun = params.get("first_run") === "true" // true when the invite page is open during a "create medusa app" run |
|
184
|
|
|
185
|
const form = useForm<z.infer<typeof CreateAccountSchema>>({ |
|
186
|
resolver: zodResolver(CreateAccountSchema), |
|
187
|
defaultValues: { |
|
188
|
email: isFirstRun ? "" : invite.email || "", |
|
189
|
first_name: "", |
|
190
|
last_name: "", |
|
191
|
password: "", |
|
192
|
repeat_password: "", |
|
193
|
}, |
|
194
|
}) |
|
195
|
|
|
196
|
const { mutateAsync: signUpEmailPass, isPending: isCreatingAuthUser } = |
|
197
|
useSignUpWithEmailPass() |
|
198
|
|
|
199
|
const { mutateAsync: logInWithEmailPass, isPending: isLoggingIn } = |
|
200
|
useSignInWithEmailPass() |
|
201
|
|
|
202
|
const { mutateAsync: acceptInvite, isPending: isAcceptingInvite } = |
|
203
|
useAcceptInvite(token) |
|
204
|
|
|
205
|
/** |
|
206
|
* Sign up or login with email and password. |
|
207
|
* If the email already exists, it will try to login instead of signing up, to allow other invited actory types to accept the invite |
|
208
|
* @param email - The email to sign up or login with. |
|
209
|
* @param password - The password to sign up or login with. |
|
210
|
* @returns The authentication result. |
|
211
|
*/ |
|
212
|
const signUpOrLoginEmailPass = async ( |
|
213
|
email: string, |
|
214
|
password: string |
|
215
|
): Promise<AuthLoginResponse | undefined> => { |
|
216
|
try { |
|
217
|
return await signUpEmailPass({ |
|
218
|
email, |
|
219
|
password, |
|
220
|
}) |
|
221
|
} catch (error) { |
|
222
|
if ( |
|
223
|
isFetchError(error) && |
|
224
|
error.status === 401 && |
|
225
|
error.message === "Identity with email already exists" |
|
226
|
) { |
|
227
|
return await logInWithEmailPass({ |
|
228
|
email, |
|
229
|
password, |
|
230
|
}) |
|
231
|
} |
|
232
|
|
|
233
|
throw error |
|
234
|
} |
|
235
|
} |
|
236
|
|
|
237
|
const handleSubmit = form.handleSubmit(async (data) => { |
|
238
|
try { |
|
239
|
const authResult = await signUpOrLoginEmailPass(data.email, data.password) |
|
240
|
|
|
241
|
// This should not happen since email verification is not enabled in the admin, but it should be covered as a scenario. |
|
242
|
if (typeof authResult !== "string") { |
|
243
|
throw new Error( |
|
244
|
"Email verification is required, but not supported by this flow." |
|
245
|
) |
|
246
|
} |
|
247
|
|
|
248
|
const authToken = authResult |
|
249
|
|
|
250
|
const invitePayload = { |
|
251
|
email: data.email, |
|
252
|
first_name: data.first_name, |
|
253
|
last_name: data.last_name, |
|
254
|
} |
|
255
|
|
|
256
|
await acceptInvite({ |
|
257
|
...invitePayload, |
|
258
|
auth_token: authToken, |
|
259
|
}) |
|
260
|
|
|
261
|
toast.success(t("invite.toast.accepted")) |
|
262
|
|
|
263
|
onSuccess() |
|
264
|
} catch (error) { |
|
265
|
if (isFetchError(error) && error.status === 400) { |
|
266
|
form.setError("root", { |
|
267
|
type: "manual", |
|
268
|
message: t("invite.invalidInvite"), |
|
269
|
}) |
|
270
|
setInvalid(true) |
|
271
|
return |
|
272
|
} |
|
273
|
|
|
274
|
form.setError("root", { |
|
275
|
type: "manual", |
|
276
|
message: t("errors.serverError"), |
|
277
|
}) |
|
278
|
} |
|
279
|
}) |
|
280
|
|
|
281
|
const serverError = form.formState.errors.root?.message |
|
282
|
const validationError = |
|
283
|
form.formState.errors.email?.message || |
|
284
|
form.formState.errors.password?.message || |
|
285
|
form.formState.errors.repeat_password?.message || |
|
286
|
form.formState.errors.first_name?.message || |
|
287
|
form.formState.errors.last_name?.message |
|
288
|
|
|
289
|
return ( |
|
290
|
<div className="flex w-full flex-col items-center"> |
|
291
|
<div className="mb-4 flex flex-col items-center"> |
|
292
|
<Heading>{t("invite.title")}</Heading> |
|
293
|
<Text size="small" className="text-ui-fg-subtle text-center"> |
|
294
|
{t("invite.hint")} |
|
295
|
</Text> |
|
296
|
</div> |
|
297
|
<Form {...form}> |
|
298
|
<form onSubmit={handleSubmit} className="flex w-full flex-col gap-y-6"> |
|
299
|
<div className="flex flex-col gap-y-2"> |
|
300
|
<Form.Field |
|
301
|
control={form.control} |
|
302
|
name="email" |
|
303
|
render={({ field }) => { |
|
304
|
return ( |
|
305
|
<Form.Item> |
|
306
|
<Form.Control> |
|
307
|
<Input |
|
308
|
autoComplete="off" |
|
309
|
{...field} |
|
310
|
className="bg-ui-bg-field-component" |
|
311
|
placeholder={t("fields.email")} |
|
312
|
/> |
|
313
|
</Form.Control> |
|
314
|
</Form.Item> |
|
315
|
) |
|
316
|
}} |
|
317
|
/> |
|
318
|
<Form.Field |
|
319
|
control={form.control} |
|
320
|
name="first_name" |
|
321
|
render={({ field }) => { |
|
322
|
return ( |
|
323
|
<Form.Item> |
|
324
|
<Form.Control> |
|
325
|
<Input |
|
326
|
autoComplete="given-name" |
|
327
|
{...field} |
|
328
|
className="bg-ui-bg-field-component" |
|
329
|
placeholder={t("fields.firstName")} |
|
330
|
/> |
|
331
|
</Form.Control> |
|
332
|
</Form.Item> |
|
333
|
) |
|
334
|
}} |
|
335
|
/> |
|
336
|
<Form.Field |
|
337
|
control={form.control} |
|
338
|
name="last_name" |
|
339
|
render={({ field }) => { |
|
340
|
return ( |
|
341
|
<Form.Item> |
|
342
|
<Form.Control> |
|
343
|
<Input |
|
344
|
autoComplete="family-name" |
|
345
|
{...field} |
|
346
|
className="bg-ui-bg-field-component" |
|
347
|
placeholder={t("fields.lastName")} |
|
348
|
/> |
|
349
|
</Form.Control> |
|
350
|
</Form.Item> |
|
351
|
) |
|
352
|
}} |
|
353
|
/> |
|
354
|
<Form.Field |
|
355
|
control={form.control} |
|
356
|
name="password" |
|
357
|
render={({ field }) => { |
|
358
|
return ( |
|
359
|
<Form.Item> |
|
360
|
<Form.Control> |
|
361
|
<Input |
|
362
|
autoComplete="new-password" |
|
363
|
type="password" |
|
364
|
{...field} |
|
365
|
className="bg-ui-bg-field-component" |
|
366
|
placeholder={t("fields.password")} |
|
367
|
/> |
|
368
|
</Form.Control> |
|
369
|
</Form.Item> |
|
370
|
) |
|
371
|
}} |
|
372
|
/> |
|
373
|
<Form.Field |
|
374
|
control={form.control} |
|
375
|
name="repeat_password" |
|
376
|
render={({ field }) => { |
|
377
|
return ( |
|
378
|
<Form.Item> |
|
379
|
<Form.Control> |
|
380
|
<Input |
|
381
|
autoComplete="off" |
|
382
|
type="password" |
|
383
|
{...field} |
|
384
|
className="bg-ui-bg-field-component" |
|
385
|
placeholder={t("fields.repeatPassword")} |
|
386
|
/> |
|
387
|
</Form.Control> |
|
388
|
</Form.Item> |
|
389
|
) |
|
390
|
}} |
|
391
|
/> |
|
392
|
{validationError && ( |
|
393
|
<div className="mt-6 text-center"> |
|
394
|
<Hint className="inline-flex" variant={"error"}> |
|
395
|
{validationError} |
|
396
|
</Hint> |
|
397
|
</div> |
|
398
|
)} |
|
399
|
{serverError && ( |
|
400
|
<Alert |
|
401
|
className="bg-ui-bg-base items-center p-2" |
|
402
|
dismissible |
|
403
|
variant="error" |
|
404
|
> |
|
405
|
{serverError} |
|
406
|
</Alert> |
|
407
|
)} |
|
408
|
</div> |
|
409
|
<Button |
|
410
|
className="w-full" |
|
411
|
type="submit" |
|
412
|
isLoading={isCreatingAuthUser || isAcceptingInvite || isLoggingIn} |
|
413
|
disabled={invalid} |
|
414
|
> |
|
415
|
{t("invite.createAccount")} |
|
416
|
</Button> |
|
417
|
</form> |
|
418
|
</Form> |
|
419
|
<LoginLink /> |
|
420
|
</div> |
|
421
|
) |
|
422
|
} |
|
423
|
|
|
424
|
const SuccessView = () => { |
|
425
|
const { t } = useTranslation() |
|
426
|
|
|
427
|
return ( |
|
428
|
<div className="flex w-full flex-col items-center gap-y-6"> |
|
429
|
<div className="flex flex-col items-center gap-y-1"> |
|
430
|
<Heading className="text-center">{t("invite.successTitle")}</Heading> |
|
431
|
<Text size="small" className="text-ui-fg-subtle text-center"> |
|
432
|
{t("invite.successHint")} |
|
433
|
</Text> |
|
434
|
</div> |
|
435
|
<Button variant="secondary" asChild className="w-full"> |
|
436
|
<Link to="/login" replace> |
|
437
|
{t("invite.successAction")} |
|
438
|
</Link> |
|
439
|
</Button> |
|
440
|
|
|
441
|
<Link |
|
442
|
key="login-link" |
|
443
|
to="/login" |
|
444
|
className="txt-small text-ui-fg-base transition-fg hover:text-ui-fg-base-hover focus-visible:text-ui-fg-base-hover font-medium outline-none" |
|
445
|
> |
|
446
|
{t("invite.backToLogin")} |
|
447
|
</Link> |
|
448
|
</div> |
|
449
|
) |
|
450
|
} |
|
451
|
|
|
452
|
const InviteSchema = z.object({ |
|
453
|
id: z.string(), |
|
454
|
jti: z.string(), |
|
455
|
exp: z.number(), |
|
456
|
iat: z.number(), |
|
457
|
}) |
|
458
|
|
|
459
|
const validateDecodedInvite = (decoded: any): decoded is DecodedInvite => { |
|
460
|
return InviteSchema.safeParse(decoded).success |
|
461
|
} |