import { Button, Center, Container, Group, Paper, PasswordInput, Stack, Tabs, Text, TextInput, Title, } from "@mantine/core"; import { useForm, yupResolver } from "@mantine/form"; import { useModals } from "@mantine/modals"; import { Tb2Fa } from "react-icons/tb"; import * as yup from "yup"; import showEnableTotpModal from "../../components/account/showEnableTotpModal"; import ThemeSwitcher from "../../components/account/ThemeSwitcher"; import Meta from "../../components/Meta"; import useUser from "../../hooks/user.hook"; import authService from "../../services/auth.service"; import userService from "../../services/user.service"; import toast from "../../utils/toast.util"; const Account = () => { const { user, refreshUser } = useUser(); const modals = useModals(); const accountForm = useForm({ initialValues: { username: user?.username, email: user?.email, }, validate: yupResolver( yup.object().shape({ email: yup.string().email(), username: yup.string().min(3), }) ), }); const passwordForm = useForm({ initialValues: { oldPassword: "", password: "", }, validate: yupResolver( yup.object().shape({ oldPassword: yup.string().min(8), password: yup.string().min(8), }) ), }); const enableTotpForm = useForm({ initialValues: { password: "", }, validate: yupResolver( yup.object().shape({ password: yup.string().min(8), }) ), }); const disableTotpForm = useForm({ initialValues: { password: "", code: "", }, validate: yupResolver( yup.object().shape({ password: yup.string().min(8), code: yup .string() .min(6) .max(6) .matches(/^[0-9]+$/, { message: "Code must be a number" }), }) ), }); return ( <> My account Account Info
userService .updateCurrentUser({ username: values.username, email: values.email, }) .then(() => toast.success("User updated successfully")) .catch(toast.axiosError) )} >
Password
authService .updatePassword(values.oldPassword, values.password) .then(() => { toast.success("Password updated successfully"); passwordForm.reset(); }) .catch(toast.axiosError) )} >
Security }> TOTP {user!.totpVerified ? ( <>
{ authService .disableTOTP(values.code, values.password) .then(() => { toast.success("Successfully disabled TOTP"); values.password = ""; values.code = ""; refreshUser(); }) .catch(toast.axiosError); })} >
) : ( <>
{ authService .enableTOTP(values.password) .then((result) => { showEnableTotpModal(modals, refreshUser, { qrCode: result.qrCode, secret: result.totpSecret, password: values.password, }); values.password = ""; }) .catch(toast.axiosError); })} >
)}
Color scheme
); }; export default Account;