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);
      }

      Typescript language logo
      your recipe card header background
      Private Route NextJS

      tag1
      tag2
      tag3
      ...

      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

        Codiga - All rights reserved 2022.