import { GetServerSidePropsContext, NextApiRequest } from "next";
import cookie from "cookie"
import { NextApiRequestCookies } from "next/dist/server/api-utils";
import { IncomingMessage } from "http";
const Private = ({ isAuthenticated }: any) => {
return <> PRIVATE ROUTE</>
}
export default Private;
export async function getServerSideProps(context: GetServerSidePropsContext) {
let isAuthenticated = false;
const cookies = parseCookies(context.req);
if (cookies.auth == "true") {
isAuthenticated = true
return {
props: { isAuthenticated },
}
}
return {
redirect: {
permanent: false,
destination: "/login",
},
props: { isAuthenticated },
};
}
function parseCookies(req: IncomingMessage & {
cookies: NextApiRequestCookies;
}) {
return cookie.parse(req ? req.headers.cookie || "" : document.cookie);
}
Private Route NextJS
The Private route defines a route that will only be accessible to authenticated users. It returns a container with a render function that will render a PRIVATE ROUTE component.
Shortcut: nextprivateroute
0 Comments
Add Comment
Log in to add a comment