import AccentSubmitButton from "@/components/AccentSubmitButton"; 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 { InferGetServerSidePropsType } from "next"; interface FormData { username: string; password: string; } export const getServerSideProps = () => { const availableLogins = getLogins(); return { props: { availableLogins } }; }; export default function Login({ availableLogins, }: InferGetServerSidePropsType) { 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("Authenticating..."); const res = await signIn("credentials", { username: form.username, password: form.password, redirect: false, }); toast.dismiss(load); setSubmitLoader(false); if (!res?.ok) { toast.error("Invalid login."); } } else { toast.error("Please fill out all the fields."); } } async function loginUserButton(method: string) { setSubmitLoader(true); const load = toast.loading("Authenticating..."); const res = await signIn(method, {}); toast.dismiss(load); setSubmitLoader(false); } function displayLoginCredential() { if (availableLogins.credentialsEnabled === "true") { return ( <>

Enter your credentials


Username {availableLogins.emailEnabled === "true" ? " or Email" : undefined}

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

Password

setForm({ ...form, password: e.target.value })} /> {availableLogins.emailEnabled === "true" && (
Forgot Password?
)}
{availableLogins.buttonAuths.length > 0 ? (
OR
) : undefined} ); } } function displayLoginExternalButton() { const Buttons: any = []; availableLogins.buttonAuths.forEach((value, index) => { Buttons.push( {index !== 0 ?
OR
: undefined} loginUserButton(value.method)} label={`Sign in with ${value.name}`} className=" w-full text-center" loading={submitLoader} />
); }); return Buttons; } function displayRegistration() { if (availableLogins.registrationDisabled !== "true") { return (

New here?

Sign Up
); } } return (
{displayLoginCredential()} {displayLoginExternalButton()} {displayRegistration()} You can install Linkwarden onto your device
); }