Skip to content

Commit

Permalink
[BarClerk-551] - [FE] Implement Route Protection (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
impaulintech authored Nov 7, 2022
1 parent df036e8 commit 883956a
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 5 deletions.
48 changes: 43 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
## BARCLERK
### API
- https://barclerk.herokuapp.com/api
### Client:
- https://barclerk.vercel.app
## BARCLERK
# Description

This tool/application aims to solve problems faced by legal practitioners in Western Australia particularly when it comes to matters from Legal Aid. Such problems include tracking the list of all the matters from Legal Aid, as well as billing.

## 🔗 Links

- Client :: https://barclerk.vercel.app
- Server :: https://barclerk.herokuapp.com/api

## Developers

- [@AJ](https://github.com/abduljalilpalala)
- [@John](https://github.com/johnpaul-sun)
- [@Joshua](https://github.com/jsvelte)

## Quality Assurance

- [@AJ](https://github.com/abduljalilpalala)
- [@John](https://github.com/johnpaul-sun)
- [@Joshua](https://github.com/jsvelte)

## License

[![MIT License](https://img.shields.io/apm/l/atomic-design-ui.svg?)](https://github.com/tterb/atomic-design-ui/blob/master/LICENSEs)
[![AGPL License](https://img.shields.io/badge/license-AGPL-blue.svg)](http://www.gnu.org/licenses/agpl-3.0)
[![GPLv3 License](https://img.shields.io/badge/License-GPL%20v3-yellow.svg)](https://opensource.org/licenses/)

## Tech Stack

**Client:**
- Next.js
- Redux Toolkit
- Redux Thunk
- TailwindCSS

**Server:**
- Laravel
- MySQL

## Appendix

Any additional information goes here
1 change: 1 addition & 0 deletions client/pages/forgot-password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,5 @@ const ForgotPassword = () => {
);
};

export { authCheck as getServerSideProps } from 'utils/getServerSideProps';
export default ForgotPassword;
1 change: 1 addition & 0 deletions client/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export default function Home() {
)
}

export { authCheck as getServerSideProps } from 'utils/getServerSideProps';
1 change: 1 addition & 0 deletions client/pages/sign-in.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@ const SignIn = () => {
);
};

export { SignInUpAuthChecker as getServerSideProps } from 'utils/getServerSideProps';
export default SignIn;
1 change: 1 addition & 0 deletions client/pages/sign-up.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ const SignUp = () => {
);
};

export { SignInUpAuthChecker as getServerSideProps } from 'utils/getServerSideProps';
export default SignUp;
83 changes: 83 additions & 0 deletions client/utils/getServerSideProps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { GetServerSideProps } from 'next';

import { setAuth } from 'redux/auth/authSlice';
import { wrapper } from 'redux/store';
import { axios } from 'shared/lib/axios';

export const SignInUpAuthChecker: GetServerSideProps =
wrapper.getServerSideProps((store) => async ({ req }) => {
const token = req.cookies['token'];
const config = { headers: { Authorization: `Bearer ${token}` } };

try {
const res = await axios.get('/auth', config);

if (res.data) {
store.dispatch(setAuth(res.data));

return {
redirect: {
permanent: false,
destination: '/',
},
props: req,
};
}
} catch (error: any) {}

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

export const authCheck: GetServerSideProps = wrapper.getServerSideProps(
(store) =>
async ({ req, params }) => {
const token = req.cookies['token'];
const config = { headers: { Authorization: `Bearer ${token}` } };

try {
const res = await axios.get('/auth', config);
store.dispatch(setAuth(res.data));

const forgotPasswordPage = req.url?.includes('forgot-password');
const linkClicked =
req.url?.includes('user') && req.url?.includes('verified');

if (forgotPasswordPage) {
if (!token) return { props: {} };
if (linkClicked) return { props: {} };

return {
redirect: {
permanent: false,
destination: '/',
},
};
}
} catch (error: any) {
const forgotPasswordPage = req.url?.includes('forgot-password');

if (forgotPasswordPage) return { props: {} };
if (error.response.status === 404) {
return {
notFound: true,
};
}
if (error.response.status === 500) {
throw new Error('Internal Server Error');
}
return {
redirect: {
permanent: false,
destination: '/sign-in',
},
props: {},
};
}

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

0 comments on commit 883956a

Please sign in to comment.