import Button from "@/components/ui/Button"; import TextInput from "@/components/TextInput"; import CenteredForm from "@/layouts/CenteredForm"; import { signIn } from "next-auth/react"; import Link from "next/link"; import React, { useState, FormEvent } from "react"; import { toast } from "react-hot-toast"; import { getLogins } from "./api/v1/logins"; import { GetServerSideProps, InferGetServerSidePropsType } from "next"; import InstallApp from "@/components/InstallApp"; import { serverSideTranslations } from "next-i18next/serverSideTranslations"; import { i18n } from "next-i18next.config"; import { getToken } from "next-auth/jwt"; import { prisma } from "@/lib/api/db"; import { useTranslation } from "next-i18next"; import { useRouter } from "next/router"; interface FormData { username: string; password: string; } export default function Login({ availableLogins, }: InferGetServerSidePropsType) { const { t } = useTranslation(); const router = useRouter(); const [submitLoader, setSubmitLoader] = useState(false); const [form, setForm] = useState({ username: "", password: "", }); async function loginUser(event: FormEvent) { event.preventDefault(); if (form.username !== "" && form.password !== "") { setSubmitLoader(true); const load = toast.loading(t("authenticating")); const res = await signIn("credentials", { username: form.username, password: form.password, redirect: false, }); toast.dismiss(load); setSubmitLoader(false); if (!res?.ok) { toast.error(res?.error || t("invalid_credentials")); if (res?.error === "Email not verified.") { await signIn("email", { email: form.username, callbackUrl: "/", redirect: false, }); router.push( `/confirmation?email=${encodeURIComponent(form.username)}` ); } } } else { toast.error(t("fill_all_fields")); } } async function loginUserButton(method: string) { setSubmitLoader(true); const load = toast.loading(t("authenticating")); const res = await signIn(method, {}); toast.dismiss(load); setSubmitLoader(false); } function displayLoginCredential() { if (availableLogins.credentialsEnabled === "true") { return ( <>

{t("enter_credentials")}


{availableLogins.emailEnabled === "true" ? t("username_or_email") : t("username")}

setForm({ ...form, username: e.target.value })} />

{t("password")}

setForm({ ...form, password: e.target.value })} /> {availableLogins.emailEnabled === "true" && (
{t("forgot_password")}
)}
{availableLogins.buttonAuths.length > 0 ? (
{t("or_continue_with")}
) : undefined} ); } } function displayLoginExternalButton() { const Buttons: any = []; availableLogins.buttonAuths.forEach((value: any, index: any) => { Buttons.push( ); }); return Buttons; } function displayRegistration() { if (availableLogins.registrationDisabled !== "true") { return (

{t("new_here")}

{t("sign_up")}
); } } return (
{displayLoginCredential()} {displayLoginExternalButton()} {displayRegistration()}
); } const getServerSideProps: GetServerSideProps = async (ctx) => { const availableLogins = getLogins(); const acceptLanguageHeader = ctx.req.headers["accept-language"]; const availableLanguages = i18n.locales; const token = await getToken({ req: ctx.req }); if (token) { const user = await prisma.user.findUnique({ where: { id: token.id, }, }); if (user) { return { props: { availableLogins, ...(await serverSideTranslations(user.locale ?? "en", ["common"])), }, }; } } const acceptedLanguages = acceptLanguageHeader ?.split(",") .map((lang) => lang.split(";")[0]); let bestMatch = acceptedLanguages?.find((lang) => availableLanguages.includes(lang) ); if (!bestMatch) { acceptedLanguages?.some((acceptedLang) => { const partialMatch = availableLanguages.find((lang) => lang.startsWith(acceptedLang) ); if (partialMatch) { bestMatch = partialMatch; return true; } return false; }); } return { props: { availableLogins, ...(await serverSideTranslations(bestMatch ?? "en", ["common"])), }, }; }; export { getServerSideProps };