import {User} from "@/interfaces/user"; import {toast, ToastContainer} from "react-toastify"; import axios from "axios"; import {FormEvent, useState} from "react"; import Head from "next/head"; import useUser from "@/hooks/useUser"; import {InputText} from "primereact/inputtext"; import {Button} from "primereact/button"; export default function Login() { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const {mutateUser} = useUser({ redirectTo: "/", redirectIfFound: true, }); const login = (e: FormEvent) => { e.preventDefault(); setIsLoading(true); axios .post("/api/login", {email, password}) .then((response) => { toast.success("You have been logged in!", {toastId: "login-successful"}); mutateUser(response.data); }) .catch((e) => { if (e.response.status === 401) { toast.error("Wrong login credentials!", {toastId: "wrong-credentials"}); } else { toast.error("Something went wrong!", {toastId: "server-error"}); } setIsLoading(false); }); }; return ( <> Login | IELTS GPT
setEmail(e.target.value)} autoComplete="username" />
setPassword(e.target.value)} autoComplete="current-password" />
); }