2022-12-05 15:53:24 +01:00
|
|
|
import {
|
|
|
|
|
Button,
|
|
|
|
|
Center,
|
|
|
|
|
Container,
|
|
|
|
|
Group,
|
|
|
|
|
Paper,
|
|
|
|
|
PasswordInput,
|
|
|
|
|
Stack,
|
2022-12-21 11:58:37 -05:00
|
|
|
Tabs,
|
2022-12-05 15:53:24 +01:00
|
|
|
Text,
|
|
|
|
|
TextInput,
|
|
|
|
|
Title,
|
|
|
|
|
} from "@mantine/core";
|
|
|
|
|
import { useForm, yupResolver } from "@mantine/form";
|
|
|
|
|
import { useModals } from "@mantine/modals";
|
2022-12-21 11:58:37 -05:00
|
|
|
import { Tb2Fa } from "react-icons/tb";
|
2023-07-20 15:32:07 +02:00
|
|
|
import { FormattedMessage } from "react-intl";
|
2022-12-05 15:53:24 +01:00
|
|
|
import * as yup from "yup";
|
2023-01-17 09:13:53 +01:00
|
|
|
import Meta from "../../components/Meta";
|
2023-07-20 15:32:07 +02:00
|
|
|
import ThemeSwitcher from "../../components/account/ThemeSwitcher";
|
|
|
|
|
import showEnableTotpModal from "../../components/account/showEnableTotpModal";
|
|
|
|
|
import useTranslate from "../../hooks/useTranslate.hook";
|
2022-12-05 15:53:24 +01:00
|
|
|
import useUser from "../../hooks/user.hook";
|
|
|
|
|
import authService from "../../services/auth.service";
|
|
|
|
|
import userService from "../../services/user.service";
|
|
|
|
|
import toast from "../../utils/toast.util";
|
2023-07-20 15:32:07 +02:00
|
|
|
import LanguagePicker from "../../components/account/LanguagePicker";
|
2022-12-05 15:53:24 +01:00
|
|
|
|
|
|
|
|
const Account = () => {
|
2023-02-07 10:21:25 +01:00
|
|
|
const { user, refreshUser } = useUser();
|
2022-12-05 15:53:24 +01:00
|
|
|
const modals = useModals();
|
2023-07-20 15:32:07 +02:00
|
|
|
const t = useTranslate();
|
2022-12-05 15:53:24 +01:00
|
|
|
|
|
|
|
|
const accountForm = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
username: user?.username,
|
|
|
|
|
email: user?.email,
|
|
|
|
|
},
|
|
|
|
|
validate: yupResolver(
|
|
|
|
|
yup.object().shape({
|
2023-07-20 15:32:07 +02:00
|
|
|
email: yup.string().email(t("common.error.invalid-email")),
|
|
|
|
|
username: yup
|
|
|
|
|
.string()
|
|
|
|
|
.min(3, t("common.error.too-short", { length: 3 })),
|
2022-12-05 15:53:24 +01:00
|
|
|
})
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const passwordForm = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
oldPassword: "",
|
|
|
|
|
password: "",
|
|
|
|
|
},
|
|
|
|
|
validate: yupResolver(
|
|
|
|
|
yup.object().shape({
|
2023-07-20 15:32:07 +02:00
|
|
|
oldPassword: yup
|
|
|
|
|
.string()
|
|
|
|
|
.min(8, t("common.error.too-short", { length: 8 }))
|
|
|
|
|
.required(t("common.error.field-required")),
|
|
|
|
|
password: yup
|
|
|
|
|
.string()
|
|
|
|
|
.min(8, t("common.error.too-short", { length: 8 }))
|
|
|
|
|
.required(t("common.error.field-required")),
|
2022-12-05 15:53:24 +01:00
|
|
|
})
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-21 11:58:37 -05:00
|
|
|
const enableTotpForm = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
password: "",
|
|
|
|
|
},
|
|
|
|
|
validate: yupResolver(
|
|
|
|
|
yup.object().shape({
|
2023-07-20 15:32:07 +02:00
|
|
|
password: yup
|
|
|
|
|
.string()
|
|
|
|
|
.min(8, t("common.error.too-short", { length: 8 }))
|
|
|
|
|
.required(t("common.error.field-required")),
|
2022-12-21 11:58:37 -05:00
|
|
|
})
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const disableTotpForm = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
password: "",
|
|
|
|
|
code: "",
|
|
|
|
|
},
|
|
|
|
|
validate: yupResolver(
|
|
|
|
|
yup.object().shape({
|
|
|
|
|
password: yup.string().min(8),
|
|
|
|
|
code: yup
|
|
|
|
|
.string()
|
2023-07-20 15:32:07 +02:00
|
|
|
.min(6, t("common.error.exact-length", { length: 6 }))
|
|
|
|
|
.max(6, t("common.error.exact-length", { length: 6 }))
|
|
|
|
|
.matches(/^[0-9]+$/, { message: t("common.error.invalid-number") }),
|
2022-12-21 11:58:37 -05:00
|
|
|
})
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-05 15:53:24 +01:00
|
|
|
return (
|
2023-01-17 09:13:53 +01:00
|
|
|
<>
|
2023-07-20 15:32:07 +02:00
|
|
|
<Meta title={t("account.title")} />
|
2023-01-17 09:13:53 +01:00
|
|
|
<Container size="sm">
|
|
|
|
|
<Title order={3} mb="xs">
|
2023-07-20 15:32:07 +02:00
|
|
|
<FormattedMessage id="account.title" />
|
2022-12-05 15:53:24 +01:00
|
|
|
</Title>
|
2023-01-17 09:13:53 +01:00
|
|
|
<Paper withBorder p="xl">
|
|
|
|
|
<Title order={5} mb="xs">
|
2023-07-20 15:32:07 +02:00
|
|
|
<FormattedMessage id="account.card.info.title" />
|
2023-01-17 09:13:53 +01:00
|
|
|
</Title>
|
|
|
|
|
<form
|
|
|
|
|
onSubmit={accountForm.onSubmit((values) =>
|
|
|
|
|
userService
|
|
|
|
|
.updateCurrentUser({
|
|
|
|
|
username: values.username,
|
|
|
|
|
email: values.email,
|
|
|
|
|
})
|
2023-07-20 15:32:07 +02:00
|
|
|
.then(() => toast.success(t("account.notify.info.success")))
|
2023-01-17 09:13:53 +01:00
|
|
|
.catch(toast.axiosError)
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<Stack>
|
|
|
|
|
<TextInput
|
2023-07-20 15:32:07 +02:00
|
|
|
label={t("account.card.info.username")}
|
2023-01-17 09:13:53 +01:00
|
|
|
{...accountForm.getInputProps("username")}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
2023-07-20 15:32:07 +02:00
|
|
|
label={t("account.card.info.email")}
|
2023-01-17 09:13:53 +01:00
|
|
|
{...accountForm.getInputProps("email")}
|
|
|
|
|
/>
|
|
|
|
|
<Group position="right">
|
2023-07-20 15:32:07 +02:00
|
|
|
<Button type="submit">
|
|
|
|
|
<FormattedMessage id="common.button.save" />
|
|
|
|
|
</Button>
|
2023-01-17 09:13:53 +01:00
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
</form>
|
|
|
|
|
</Paper>
|
|
|
|
|
<Paper withBorder p="xl" mt="lg">
|
|
|
|
|
<Title order={5} mb="xs">
|
2023-07-20 15:32:07 +02:00
|
|
|
<FormattedMessage id="account.card.password.title" />
|
2023-01-17 09:13:53 +01:00
|
|
|
</Title>
|
|
|
|
|
<form
|
|
|
|
|
onSubmit={passwordForm.onSubmit((values) =>
|
|
|
|
|
authService
|
|
|
|
|
.updatePassword(values.oldPassword, values.password)
|
|
|
|
|
.then(() => {
|
2023-07-20 15:32:07 +02:00
|
|
|
toast.success(t("account.notify.password.success"));
|
2023-01-17 09:13:53 +01:00
|
|
|
passwordForm.reset();
|
|
|
|
|
})
|
|
|
|
|
.catch(toast.axiosError)
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<Stack>
|
|
|
|
|
<PasswordInput
|
2023-07-20 15:32:07 +02:00
|
|
|
label={t("account.card.password.old")}
|
2023-01-17 09:13:53 +01:00
|
|
|
{...passwordForm.getInputProps("oldPassword")}
|
|
|
|
|
/>
|
|
|
|
|
<PasswordInput
|
2023-07-20 15:32:07 +02:00
|
|
|
label={t("account.card.password.new")}
|
2023-01-17 09:13:53 +01:00
|
|
|
{...passwordForm.getInputProps("password")}
|
|
|
|
|
/>
|
|
|
|
|
<Group position="right">
|
2023-07-20 15:32:07 +02:00
|
|
|
<Button type="submit">
|
|
|
|
|
<FormattedMessage id="common.button.save" />
|
|
|
|
|
</Button>
|
2023-01-17 09:13:53 +01:00
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
</form>
|
|
|
|
|
</Paper>
|
2022-12-21 11:58:37 -05:00
|
|
|
|
2023-01-17 09:13:53 +01:00
|
|
|
<Paper withBorder p="xl" mt="lg">
|
|
|
|
|
<Title order={5} mb="xs">
|
2023-07-20 15:32:07 +02:00
|
|
|
<FormattedMessage id="account.card.security.title" />
|
2023-01-17 09:13:53 +01:00
|
|
|
</Title>
|
2022-12-21 11:58:37 -05:00
|
|
|
|
2023-01-17 09:13:53 +01:00
|
|
|
<Tabs defaultValue="totp">
|
|
|
|
|
<Tabs.List>
|
|
|
|
|
<Tabs.Tab value="totp" icon={<Tb2Fa size={14} />}>
|
|
|
|
|
TOTP
|
|
|
|
|
</Tabs.Tab>
|
|
|
|
|
</Tabs.List>
|
2022-12-21 11:58:37 -05:00
|
|
|
|
2023-01-17 09:13:53 +01:00
|
|
|
<Tabs.Panel value="totp" pt="xs">
|
2023-02-04 18:12:49 +01:00
|
|
|
{user!.totpVerified ? (
|
2023-01-17 09:13:53 +01:00
|
|
|
<>
|
|
|
|
|
<form
|
|
|
|
|
onSubmit={disableTotpForm.onSubmit((values) => {
|
|
|
|
|
authService
|
|
|
|
|
.disableTOTP(values.code, values.password)
|
|
|
|
|
.then(() => {
|
2023-07-20 15:32:07 +02:00
|
|
|
toast.success(t("account.notify.totp.disable"));
|
2023-01-17 09:13:53 +01:00
|
|
|
values.password = "";
|
|
|
|
|
values.code = "";
|
|
|
|
|
refreshUser();
|
|
|
|
|
})
|
|
|
|
|
.catch(toast.axiosError);
|
|
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
<Stack>
|
|
|
|
|
<PasswordInput
|
2023-07-20 15:32:07 +02:00
|
|
|
description={t(
|
|
|
|
|
"account.card.security.totp.disable.description"
|
|
|
|
|
)}
|
|
|
|
|
label={t("account.card.password.title")}
|
2023-01-17 09:13:53 +01:00
|
|
|
{...disableTotpForm.getInputProps("password")}
|
|
|
|
|
/>
|
2022-12-21 11:58:37 -05:00
|
|
|
|
2023-01-17 09:13:53 +01:00
|
|
|
<TextInput
|
|
|
|
|
variant="filled"
|
2023-07-20 15:32:07 +02:00
|
|
|
label={t("account.modal.totp.code")}
|
2023-01-17 09:13:53 +01:00
|
|
|
placeholder="******"
|
|
|
|
|
{...disableTotpForm.getInputProps("code")}
|
|
|
|
|
/>
|
2022-12-21 11:58:37 -05:00
|
|
|
|
2023-01-17 09:13:53 +01:00
|
|
|
<Group position="right">
|
|
|
|
|
<Button color="red" type="submit">
|
2023-07-20 15:32:07 +02:00
|
|
|
<FormattedMessage id="common.button.disable" />
|
2023-01-17 09:13:53 +01:00
|
|
|
</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
</form>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<form
|
|
|
|
|
onSubmit={enableTotpForm.onSubmit((values) => {
|
|
|
|
|
authService
|
|
|
|
|
.enableTOTP(values.password)
|
|
|
|
|
.then((result) => {
|
|
|
|
|
showEnableTotpModal(modals, refreshUser, {
|
|
|
|
|
qrCode: result.qrCode,
|
|
|
|
|
secret: result.totpSecret,
|
|
|
|
|
password: values.password,
|
|
|
|
|
});
|
|
|
|
|
values.password = "";
|
|
|
|
|
})
|
|
|
|
|
.catch(toast.axiosError);
|
|
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
<Stack>
|
|
|
|
|
<PasswordInput
|
2023-07-20 15:32:07 +02:00
|
|
|
label={t("account.card.password.title")}
|
|
|
|
|
description={t(
|
|
|
|
|
"account.card.security.totp.enable.description"
|
|
|
|
|
)}
|
2023-01-17 09:13:53 +01:00
|
|
|
{...enableTotpForm.getInputProps("password")}
|
|
|
|
|
/>
|
|
|
|
|
<Group position="right">
|
2023-07-20 15:32:07 +02:00
|
|
|
<Button type="submit">
|
|
|
|
|
<FormattedMessage id="account.card.security.totp.button.start" />
|
|
|
|
|
</Button>
|
2023-01-17 09:13:53 +01:00
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
</form>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</Tabs.Panel>
|
|
|
|
|
</Tabs>
|
|
|
|
|
</Paper>
|
|
|
|
|
<Paper withBorder p="xl" mt="lg">
|
|
|
|
|
<Title order={5} mb="xs">
|
2023-07-20 15:32:07 +02:00
|
|
|
<FormattedMessage id="account.card.language.title" />
|
|
|
|
|
</Title>
|
|
|
|
|
<LanguagePicker />
|
|
|
|
|
</Paper>
|
|
|
|
|
<Paper withBorder p="xl" mt="lg">
|
|
|
|
|
<Title order={5} mb="xs">
|
|
|
|
|
<FormattedMessage id="account.card.color.title" />
|
2023-01-17 09:13:53 +01:00
|
|
|
</Title>
|
|
|
|
|
<ThemeSwitcher />
|
|
|
|
|
</Paper>
|
|
|
|
|
<Center mt={80} mb="lg">
|
|
|
|
|
<Stack>
|
|
|
|
|
<Button
|
|
|
|
|
variant="light"
|
|
|
|
|
color="red"
|
|
|
|
|
onClick={() =>
|
|
|
|
|
modals.openConfirmModal({
|
2023-07-20 15:32:07 +02:00
|
|
|
title: t("account.modal.delete.title"),
|
2023-01-17 09:13:53 +01:00
|
|
|
children: (
|
|
|
|
|
<Text size="sm">
|
2023-07-20 15:32:07 +02:00
|
|
|
<FormattedMessage id="account.modal.delete.description" />
|
2023-01-17 09:13:53 +01:00
|
|
|
</Text>
|
|
|
|
|
),
|
2022-12-21 11:58:37 -05:00
|
|
|
|
2023-07-20 15:32:07 +02:00
|
|
|
labels: {
|
|
|
|
|
confirm: t("common.button.delete"),
|
|
|
|
|
cancel: t("common.button.cancel"),
|
|
|
|
|
},
|
2023-01-17 09:13:53 +01:00
|
|
|
confirmProps: { color: "red" },
|
|
|
|
|
onConfirm: async () => {
|
|
|
|
|
await userService.removeCurrentUser();
|
|
|
|
|
window.location.reload();
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
>
|
2023-07-20 15:32:07 +02:00
|
|
|
<FormattedMessage id="account.button.delete" />
|
2023-01-17 09:13:53 +01:00
|
|
|
</Button>
|
|
|
|
|
</Stack>
|
|
|
|
|
</Center>
|
|
|
|
|
</Container>
|
|
|
|
|
</>
|
2022-12-05 15:53:24 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Account;
|