|
1
|
import { zodResolver } from "@hookform/resolvers/zod" |
|
2
|
import type { AuthTypes } from "@medusajs/types" |
|
3
|
import { Alert, Button, Hint, Input } from "@medusajs/ui" |
|
4
|
import { useForm } from "react-hook-form" |
|
5
|
import { useTranslation } from "react-i18next" |
|
6
|
import { useLocation, useNavigate } from "react-router-dom" |
|
7
|
import * as z from "zod" |
|
8
|
|
|
9
|
import { Form } from "../../../components/common/form" |
|
10
|
import { useSignInWithEmailPass } from "../../../hooks/api" |
|
11
|
import { isFetchError } from "../../../lib/is-fetch-error" |
|
12
|
|
|
13
|
const EmailPassSchema = z.object({ |
|
14
|
email: z.string().email(), |
|
15
|
password: z.string(), |
|
16
|
}) |
|
17
|
|
|
18
|
type EmailPassLoginProps = { |
|
19
|
onMfaChallenge?: ( |
|
20
|
challenge: AuthTypes.AuthMfaChallengeDTO, |
|
21
|
onSuccess: (token: string) => void | Promise<void> |
|
22
|
) => void |
|
23
|
} |
|
24
|
|
|
25
|
export const EmailPassLogin = ({ onMfaChallenge }: EmailPassLoginProps) => { |
|
26
|
const { t } = useTranslation() |
|
27
|
const location = useLocation() |
|
28
|
const navigate = useNavigate() |
|
29
|
|
|
30
|
const from = location.state?.from?.pathname || "/orders" |
|
31
|
|
|
32
|
const form = useForm<z.infer<typeof EmailPassSchema>>({ |
|
33
|
resolver: zodResolver(EmailPassSchema), |
|
34
|
defaultValues: { |
|
35
|
email: "", |
|
36
|
password: "", |
|
37
|
}, |
|
38
|
}) |
|
39
|
|
|
40
|
const { mutateAsync, isPending } = useSignInWithEmailPass() |
|
41
|
|
|
42
|
const handleSubmit = form.handleSubmit(async ({ email, password }) => { |
|
43
|
await mutateAsync( |
|
44
|
{ |
|
45
|
email, |
|
46
|
password, |
|
47
|
}, |
|
48
|
{ |
|
49
|
onError: (error) => { |
|
50
|
if (isFetchError(error)) { |
|
51
|
if (error.status === 401) { |
|
52
|
form.setError("email", { |
|
53
|
type: "manual", |
|
54
|
message: error.message, |
|
55
|
}) |
|
56
|
|
|
57
|
return |
|
58
|
} |
|
59
|
} |
|
60
|
|
|
61
|
form.setError("root.serverError", { |
|
62
|
type: "manual", |
|
63
|
message: error.message, |
|
64
|
}) |
|
65
|
}, |
|
66
|
onSuccess: (result) => { |
|
67
|
if (typeof result === "object" && "mfa_challenge" in result) { |
|
68
|
onMfaChallenge?.(result.mfa_challenge, () => { |
|
69
|
navigate(from, { replace: true }) |
|
70
|
}) |
|
71
|
return |
|
72
|
} |
|
73
|
|
|
74
|
navigate(from, { replace: true }) |
|
75
|
}, |
|
76
|
} |
|
77
|
) |
|
78
|
}) |
|
79
|
|
|
80
|
const serverError = form.formState.errors?.root?.serverError?.message |
|
81
|
const validationError = |
|
82
|
form.formState.errors.email?.message || |
|
83
|
form.formState.errors.password?.message |
|
84
|
|
|
85
|
return ( |
|
86
|
<Form {...form}> |
|
87
|
<form onSubmit={handleSubmit} className="flex w-full flex-col gap-y-6"> |
|
88
|
<div className="flex flex-col gap-y-1"> |
|
89
|
<Form.Field |
|
90
|
control={form.control} |
|
91
|
name="email" |
|
92
|
render={({ field }) => { |
|
93
|
return ( |
|
94
|
<Form.Item> |
|
95
|
<Form.Control> |
|
96
|
<Input |
|
97
|
autoComplete="email" |
|
98
|
{...field} |
|
99
|
className="bg-ui-bg-field-component" |
|
100
|
placeholder={t("fields.email")} |
|
101
|
/> |
|
102
|
</Form.Control> |
|
103
|
</Form.Item> |
|
104
|
) |
|
105
|
}} |
|
106
|
/> |
|
107
|
<Form.Field |
|
108
|
control={form.control} |
|
109
|
name="password" |
|
110
|
render={({ field }) => { |
|
111
|
return ( |
|
112
|
<Form.Item> |
|
113
|
<Form.Label>{}</Form.Label> |
|
114
|
<Form.Control> |
|
115
|
<Input |
|
116
|
type="password" |
|
117
|
autoComplete="current-password" |
|
118
|
{...field} |
|
119
|
className="bg-ui-bg-field-component" |
|
120
|
placeholder={t("fields.password")} |
|
121
|
/> |
|
122
|
</Form.Control> |
|
123
|
</Form.Item> |
|
124
|
) |
|
125
|
}} |
|
126
|
/> |
|
127
|
</div> |
|
128
|
{validationError && ( |
|
129
|
<div className="text-center"> |
|
130
|
<Hint className="inline-flex" variant={"error"}> |
|
131
|
{validationError} |
|
132
|
</Hint> |
|
133
|
</div> |
|
134
|
)} |
|
135
|
{serverError && ( |
|
136
|
<Alert |
|
137
|
className="bg-ui-bg-base items-center p-2" |
|
138
|
dismissible |
|
139
|
variant="error" |
|
140
|
> |
|
141
|
{serverError} |
|
142
|
</Alert> |
|
143
|
)} |
|
144
|
<Button className="w-full" type="submit" isLoading={isPending}> |
|
145
|
{t("actions.continueWithEmail")} |
|
146
|
</Button> |
|
147
|
</form> |
|
148
|
</Form> |
|
149
|
) |
|
150
|
} |