Skip to content

Commit

Permalink
Fix: fix fredirect after login, if directly accessing the login page
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilalekha committed Jul 26, 2021
1 parent 0234892 commit 2705ea7
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions pages/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { LoadIcon } from "../component/ButtonLoader/LoadIcon";
import Head from "next/head";
import Link from "next/link";
import Fade from "react-reveal/Fade";
import { getCookieValue } from "../lib/cookie";

const axios = require("axios");

export async function getServerSideProps(context) {
// console.log(context.req.headers.referer);
try {
if (context.req.headers.cookie) {
const cookie = JSON.parse(
Expand All @@ -28,14 +30,23 @@ export async function getServerSideProps(context) {
}
}
} catch (err) {
console.log(err);
console.log("User not logged in");
return {
redirect: {
permanent: false,
destination: "/"
}
};
}
return {
props: {}
props: {
referer: context.req.headers.referer ? context.req.headers.referer : ""
}
};
}

export default function Login() {
export default function Login({ referer }) {
const router = useRouter();
const [validEmail, setEmailValid] = useState(true);
const [validPassword, setValidPassword] = useState(true);
Expand Down Expand Up @@ -140,7 +151,11 @@ export default function Login() {
.catch((err) => {
console.log(err.message);
});
router.back();
if (referer) {
router.back();
} else {
router.push("/");
}
} else {
setIsLoading(false);
notify(data);
Expand Down

0 comments on commit 2705ea7

Please sign in to comment.