Skip to content

Commit

Permalink
refactor: improve auth handling and route protection
Browse files Browse the repository at this point in the history
  • Loading branch information
atrincas committed Jan 7, 2025
1 parent 666a133 commit 7217e23
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 26 deletions.
29 changes: 22 additions & 7 deletions client/src/hoc/auth.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { GetServerSideProps, GetServerSidePropsContext } from 'next';
import { getServerSession } from 'next-auth/next';

import { authOptions } from 'pages/api/auth/[...nextauth]';
import { auth } from 'pages/api/auth/[...nextauth]';

export function withAuth(gssp: GetServerSideProps) {
export function withAuth(gssp?: GetServerSideProps) {
return async (context: GetServerSidePropsContext) => {
const session = await getServerSession(context.req, context.res, authOptions);
const session = await auth(context.req, context.res);
const isPublicRoute = context.req.url?.includes('/auth/');

if (!session) {
if (!session && !isPublicRoute) {
// Protected route without session -> redirect to signin
return {
redirect: {
destination: '/auth/signin',
Expand All @@ -16,10 +17,24 @@ export function withAuth(gssp: GetServerSideProps) {
};
}

const gsspData = await gssp(context);
if (session && isPublicRoute) {
// Public route (auth pages) with session -> redirect to projects
return {
redirect: {
destination: '/projects',
permanent: false,
},
};
}

if (gssp) {
const gsspData = await gssp(context);

return gsspData;
}

return {
...gsspData,
props: {},
};
};
}
13 changes: 12 additions & 1 deletion client/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios from 'axios';
import NextAuth from 'next-auth';
import { NextApiRequest, NextApiResponse } from 'next';
import { GetServerSidePropsContext } from 'next';
import NextAuth, { getServerSession } from 'next-auth';
import type { NextAuthOptions } from 'next-auth';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { JWT } from 'next-auth/jwt';
Expand Down Expand Up @@ -74,4 +76,13 @@ export const authOptions: NextAuthOptions = {
},
};

export function auth(
...args:
| [GetServerSidePropsContext['req'], GetServerSidePropsContext['res']]
| [NextApiRequest, NextApiResponse]
| []
) {
return getServerSession(...args, authOptions);
}

export default NextAuth(authOptions);
4 changes: 4 additions & 0 deletions client/src/pages/auth/change-password/[token]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { useParams } from 'next/navigation';

import ChangePassword from 'containers/auth/change-password';

import { withAuth } from 'hoc/auth';

const ChangePasswordPage = () => {
const params = useParams<{ token: string } | null>();

return <ChangePassword token={params?.token} />;
};

export const getServerSideProps = withAuth();

export default ChangePasswordPage;
3 changes: 3 additions & 0 deletions client/src/pages/auth/forgot-password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AuthWrapper } from 'containers/wrapper/component';
import Button from 'components/button';
import LinkButton from 'components/button/component';
import { Input } from 'components/forms';
import { withAuth } from 'hoc/auth';

import authenticationService from 'services/authentication';

Expand Down Expand Up @@ -76,4 +77,6 @@ const ForgotPasswordPage = () => {
);
};

export const getServerSideProps = withAuth();

export default ForgotPasswordPage;
20 changes: 2 additions & 18 deletions client/src/pages/auth/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import Link from 'next/link';
import { useRouter } from 'next/router';

import { FORM_ERROR } from 'final-form';
import { getServerSession } from 'next-auth/next';
import { signIn } from 'next-auth/react';

import { AuthWrapper } from 'containers/wrapper/component';

import Button from 'components/button';
import { Input } from 'components/forms/input/component';
import { authOptions } from 'pages/api/auth/[...nextauth]';
import { withAuth } from 'hoc/auth';

const SignInPage: React.FC = () => {
const router = useRouter();
Expand Down Expand Up @@ -86,21 +85,6 @@ const SignInPage: React.FC = () => {
);
};

export async function getServerSideProps(context) {
const session = await getServerSession(context.req, context.res, authOptions);

if (session) {
return {
redirect: {
destination: '/projects',
permanent: false,
},
};
}

return {
props: {},
};
}
export const getServerSideProps = withAuth();

export default SignInPage;
4 changes: 4 additions & 0 deletions client/src/pages/auth/signup/[token]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { useParams } from 'next/navigation';

import ChangePassword from 'containers/auth/change-password';

import { withAuth } from 'hoc/auth';

const SignupPage = () => {
const params = useParams<{ token: string } | null>();

return <ChangePassword token={params?.token} />;
};

export const getServerSideProps = withAuth();

export default SignupPage;
Empty file.

0 comments on commit 7217e23

Please sign in to comment.