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 login page to Home page if the user is logged in
Browse files Browse the repository at this point in the history
  ## what
  - redirect login page to home page IF the user is logged in

  ## how
  - using the function `withSessionSsr` in `src/lib/AuthSession/index.ts`
    - check if a session cookie from iron-session is set
    - if it's set, redirect to the home page

  ## why
  - no need to display the login page IF the user is already logged in.

  ## where

  ## usage
  • Loading branch information
Clumsy-Coder committed Aug 20, 2023
1 parent c91484e commit 2b5c2b4
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/pages/login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import Grid from '@mui/material/Grid';
import SvgIcon from '@mui/material/SvgIcon';
import Typography from '@mui/material/Typography';
import { withIronSessionSsr } from 'iron-session/next';
import { GetServerSidePropsContext, NextPage } from 'next';

import PiholeLogo from '@svg/pihole-logo.svg';
import LoginForm from '@components/LoginForm';
import { sessionOptions } from '@lib/AuthSession/index';
import { GetServerSidePropsContext } from 'next';
import { withSessionSsr } from '@lib/AuthSession/index';

/**
* Functional component for login page
* @returns Functional Component for Login page
*/
const PageLogin = () => (
const PageLogin: NextPage = () => (
<Grid container direction='column' justifyContent='center' alignItems='center' spacing={2}>
<Grid item>
{/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment */}
Expand All @@ -27,4 +26,28 @@ const PageLogin = () => (
</Grid>
);

/**
* Server side rendering function.
*
* If the user is logged in, it will redirect to the home page
*/
export const getServerSideProps = withSessionSsr((context: GetServerSidePropsContext) => {
const { authSession } = context.req.session;

// redirect to home page if the user is logged in
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (authSession) {
return {
redirect: {
destination: '/',
permanent: true,
},
};
}

return {
props: {},
};
});

export default PageLogin;

0 comments on commit 2b5c2b4

Please sign in to comment.