Skip to content

Commit

Permalink
Fix: Use serversideprops to redirect to login if not logged in
Browse files Browse the repository at this point in the history
- use else case in write route
  • Loading branch information
akhilalekha committed Jun 28, 2021
1 parent 325ffd9 commit 7c77134
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
41 changes: 37 additions & 4 deletions pages/posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,45 @@ import MyBlogs from "../../component/myblogs/MyBlogs";
import Head from "next/head";
import SiteHeader from "../../component/layout/SiteHeader/SiteHeader";
import Cookies from "universal-cookie";
import withAuth from "../../component/withAuth/withAuth";
import PostService from "../../services/PostService";
import Pagination from "../../component/pagination/pagination";
import MyBlogsstyles from "../../styles/MyBlogs.module.css";
import { getCookieValue } from "../../lib/cookie";

const MyPost = () => {
export async function getServerSideProps(context) {
// console.log(context.req.headers.cookie);
try {
if (context.req.headers.cookie) {
// console.log(context.req.headers.cookie);
const cookie = JSON.parse(
getCookieValue(context.req.headers.cookie, "userNullcast")
);
// console.log({ cookie });
return {
props: {}
};
} else {
return {
redirect: {
permanent: false,
destination: "/login"
}
};
}
} catch (err) {
console.log("User not logged in ");
console.log(err);
return {
redirect: {
permanent: false,
destination: "/login"
}
// props: {}
};
}
}

export default function Posts() {
const cookies = new Cookies();
const userCookie = cookies.get("userNullcast");
// console.log(userCookie);
Expand Down Expand Up @@ -98,6 +131,6 @@ const MyPost = () => {
</div>
</div>
);
};
}

export default withAuth(MyPost);
// export default withAuth(MyPost);
10 changes: 8 additions & 2 deletions pages/posts/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export async function getServerSideProps(context) {
);
// console.log(cookie);
return { props: { post_Id: post_Id ? post_Id : "" } };
} else {
return {
redirect: {
permanent: false,
destination: "/login"
}
};
}
} catch (err) {
console.log("User not logged in ");
Expand Down Expand Up @@ -61,13 +68,12 @@ export default function Write({ post_Id }) {
// !END

iframeElement.onload = function () {

if (currentPostId) {
getPostById(currentPostId);
}
};
} catch (error) {
console.log("error blaaaah==>", error)
console.log("error blaaaah==>", error);
getPostById(currentPostId);
}
}
Expand Down

0 comments on commit 7c77134

Please sign in to comment.