|
|
@@ -3,18 +3,24 @@ import { useContext, useEffect } from "react";
|
|
|
import Layout from "../components/Layout";
|
|
|
import { UserContext } from "../context/UserContext";
|
|
|
import useForm from "../hooks/useForm";
|
|
|
+import { useRouter } from "next/router";
|
|
|
import api from "../utils/api";
|
|
|
|
|
|
const Login: NextPage = () => {
|
|
|
const context = useContext(UserContext);
|
|
|
+ const router = useRouter();
|
|
|
|
|
|
const { error, submitHandler, success } = useForm(api.login);
|
|
|
|
|
|
useEffect(() => {
|
|
|
if (success && context) {
|
|
|
- console.log(success.message, success.result, context);
|
|
|
+ context.setLogged(true);
|
|
|
+ context.setUser(success.result);
|
|
|
+ setTimeout(() => {
|
|
|
+ router.push("/");
|
|
|
+ }, 1000);
|
|
|
}
|
|
|
- }, [success, context]);
|
|
|
+ }, [success, context, router]);
|
|
|
|
|
|
return (
|
|
|
<Layout>
|
|
|
@@ -40,6 +46,7 @@ const Login: NextPage = () => {
|
|
|
type="password"
|
|
|
name="password"
|
|
|
id="password"
|
|
|
+ minLength={6}
|
|
|
className="form-input"
|
|
|
placeholder="••••••"
|
|
|
required
|
|
|
@@ -48,11 +55,16 @@ const Login: NextPage = () => {
|
|
|
<button type="submit" className="form-button">
|
|
|
Log in
|
|
|
</button>
|
|
|
- {error && (
|
|
|
+ {error && !success && (
|
|
|
<div role="alert" className="form-alert-msg">
|
|
|
{error.message}
|
|
|
</div>
|
|
|
)}
|
|
|
+ {success && (
|
|
|
+ <div role="alert" className="form-success-msg">{`${
|
|
|
+ success.message || "Logged in successfully!"
|
|
|
+ } Redirecting to home`}</div>
|
|
|
+ )}
|
|
|
</form>
|
|
|
</Layout>
|
|
|
);
|