Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
feat(page): redirect Home page to Login page IF not logged in
Browse files Browse the repository at this point in the history
  ## what
  - Redirect Home page to Login page IF the user is NOT logged in

  ## how
  - use function `withAuthSsr` from `src/lib/AuthSession/index.ts`
    - the function will check if the session cookie from iron-session
      exists,
    - if the session cookie doesn't exist, then redirect to Login page
    - otherwise execute getServerSideProps with the provided context
      parameters

  ## why
  - This will have a mechenisim to protect a page route
  - the page route will be used to display content from upstream that
    requires user authentication

  ## where

  ## usage
  • Loading branch information
Clumsy-Coder committed Aug 20, 2023
1 parent 2b5c2b4 commit 44d456e
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NextPage } from 'next';
import type { GetServerSideProps, NextPage } from 'next';
import Head from 'next/head';
import Image from 'next/image';
import { withAuthSsr } from '@lib/AuthSession';
import styles from '../styles/Home.module.css';

const Home: NextPage = () => {
Expand Down Expand Up @@ -63,4 +64,13 @@ const Home: NextPage = () => {
);
};

// use wrapping function to check if user is authenticated.
// if the user is NOT authenticated, redirect to the login page
// obtained from https://stackoverflow.com/a/70737180/3053548
export const getServerSideProps: GetServerSideProps = withAuthSsr(() => {
return {
props: {},
};
});

export default Home;

0 comments on commit 44d456e

Please sign in to comment.