import { Anchor, Button, Container, createStyles, Group, Paper, PasswordInput, Stack, Text, TextInput, Title, } from "@mantine/core"; import { useForm, yupResolver } from "@mantine/form"; import { showNotification } from "@mantine/notifications"; import Link from "next/link"; import { useRouter } from "next/router"; import React from "react"; import { TbInfoCircle } from "react-icons/tb"; import { FormattedMessage } from "react-intl"; import * as yup from "yup"; import useConfig from "../../hooks/config.hook"; import useUser from "../../hooks/user.hook"; import useTranslate from "../../hooks/useTranslate.hook"; import authService from "../../services/auth.service"; import { getOAuthIcon, getOAuthUrl } from "../../utils/oauth.util"; import toast from "../../utils/toast.util"; const webroot = process.env.WEBROOT || ""; const useStyles = createStyles((theme) => ({ or: { "&:before": { content: "''", flex: 1, display: "block", borderTopWidth: 1, borderTopStyle: "solid", borderColor: theme.colorScheme === "dark" ? theme.colors.dark[3] : theme.colors.gray[4], }, "&:after": { content: "''", flex: 1, display: "block", borderTopWidth: 1, borderTopStyle: "solid", borderColor: theme.colorScheme === "dark" ? theme.colors.dark[3] : theme.colors.gray[4], }, }, })); const SignInForm = ({ redirectPath }: { redirectPath: string }) => { const config = useConfig(); const router = useRouter(); const t = useTranslate(); const { refreshUser } = useUser(); const { classes } = useStyles(); const [oauth, setOAuth] = React.useState([]); const validationSchema = yup.object().shape({ emailOrUsername: yup.string().required(t("common.error.field-required")), password: yup .string() .min(8, t("common.error.too-short", { length: 8 })) .required(t("common.error.field-required")), }); const form = useForm({ initialValues: { emailOrUsername: "", password: "", }, validate: yupResolver(validationSchema), }); const signIn = async (email: string, password: string) => { await authService .signIn(email, password) .then(async (response) => { if (response.data["loginToken"]) { // Prompt the user to enter their totp code showNotification({ icon: , color: "blue", radius: "md", title: t("signIn.notify.totp-required.title"), message: t("signIn.notify.totp-required.description"), }); router.push( `/auth/totp/${ response.data["loginToken"] }?redirect=${encodeURIComponent(redirectPath)}`, ); } else { await refreshUser(); router.replace(redirectPath); } }) .catch(toast.axiosError); }; const getAvailableOAuth = async () => { const oauth = await authService.getAvailableOAuth(); setOAuth(oauth.data); }; React.useEffect(() => { getAvailableOAuth().catch(toast.axiosError); }, []); return ( <FormattedMessage id="signin.title" /> {config.get("share.allowRegistration") && ( {" "} )}
{ signIn(values.emailOrUsername, values.password); })} > {config.get("smtp.enabled") && ( )} {oauth.length > 0 && ( {t("signIn.oauth.or")} {oauth.map((provider) => ( ))} )}
); }; export default SignInForm;