-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathLogin.tsx
More file actions
executable file
·144 lines (135 loc) · 4.7 KB
/
Login.tsx
File metadata and controls
executable file
·144 lines (135 loc) · 4.7 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
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import EmailIcon from "@mui/icons-material/Email";
import KeyIcon from "@mui/icons-material/Key";
import KeyboardArrowRightIcon from "@mui/icons-material/KeyboardArrowRight";
import { Button, Grid, Paper, Typography } from "@mui/material";
import Link from "next/link";
import { useRouter } from "next/router";
import { useEffect } from "react";
import { useConfirmAccount, useLogin } from "@/hooks/entities/auth-hooks";
import { useAuth } from "@/hooks/useAuth";
import { useForm } from "@/hooks/useForm";
import { useToast } from "@/hooks/useToast";
import { useTranslate } from "@/hooks/useTranslate";
import { ILoginAttributes } from "@/types/auth/login.types";
import { PublicContentWrapper } from "../../components/anonymous/PublicContentWrapper";
import { ContentContainer } from "../dialogs/layouts/ContentContainer";
import { Adornment } from "../inputs/Adornment";
import { Input } from "../inputs/Input";
import { PasswordInput } from "../inputs/PasswordInput";
const DEFAULT_VALUES: ILoginAttributes = {
identifier: "",
password: "",
};
export const Login = () => {
const { t } = useTranslate();
const { toast } = useToast();
const router = useRouter();
const { authenticate } = useAuth();
const { mutate: login, isLoading } = useLogin({
onSuccess: (data) => {
if (data.state) authenticate(data);
else {
toast.error(t("message.account_disabled"));
}
},
onError() {
toast.error(t("message.login_failure"));
},
});
const { mutate: confirmAccount } = useConfirmAccount({
onSuccess: () => {
toast.success(t("message.reset_confirm_success"));
},
onError: () => {
//TODO: need to enhance the error
toast.error(t("message.account_disabled"));
},
});
const {
register,
formState: { errors },
handleSubmit,
} = useForm<ILoginAttributes>({
defaultValues: DEFAULT_VALUES,
rules: {
identifier: {
required: t("message.email_is_required"),
},
password: {
required: t("message.password_is_required"),
},
},
});
const onSubmitForm = (data: ILoginAttributes) => {
login(data);
};
useEffect(() => {
const queryToken = router.query.token;
if (queryToken)
confirmAccount({
token: String(queryToken),
});
}, [router.query.token]);
return (
<PublicContentWrapper>
<Paper sx={{ width: { xs: "100%", md: "33%" }, p: 2 }}>
<form onSubmit={handleSubmit(onSubmitForm)}>
<ContentContainer gap={2}>
<Typography variant="h1" fontSize="19px" fontWeight={700}>
{t("title.login")}
</Typography>
<Input
label={t("placeholder.email")}
error={!!errors.identifier}
required
autoFocus
InputProps={{
startAdornment: <Adornment Icon={EmailIcon} />,
}}
helperText={errors.identifier ? errors.identifier.message : null}
{...register("identifier")}
/>
<PasswordInput
label={t("label.password")}
error={!!errors.password}
required
InputProps={{
startAdornment: <Adornment Icon={KeyIcon} />,
}}
helperText={errors.password ? errors.password.message : null}
{...register("password")}
/>
<Grid container gap={2} justifyContent="space-between">
<Grid alignContent="center">
<Link href="/reset">
<Button variant="text" sx={{ textDecoration: "underline" }}>
{t("link.reset")}
</Button>
</Link>
</Grid>
<Grid>
<Button
color="primary"
variant="contained"
type="submit"
endIcon={<KeyboardArrowRightIcon />}
onClick={handleSubmit(onSubmitForm)}
disabled={isLoading}
>
{t("button.login")}
</Button>
</Grid>
</Grid>
</ContentContainer>
</form>
</Paper>
</PublicContentWrapper>
);
};