Skip to content

Commit 8f335ad

Browse files
authored
Merge pull request #4 from thaind97git/develop
Add auth feature to support admin page
2 parents b068fe8 + 9cd102f commit 8f335ad

File tree

6 files changed

+57
-55
lines changed

6 files changed

+57
-55
lines changed

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ For Production:
209209
}, [fetchUser]);
210210
```
211211

212+
- How to use Auth feature
213+
- If you want to build an admin page, with authorize full layout, just go to line **50** at `src/layouts/main.tsx` and use the code inside comment.
214+
- **Auth** component is a feature help authorize page by fetching profile each time page rendered.
215+
- You must have a server API to use this feature. This API with simple function like receive a header token, return user profile if token is valid and return 404 if token is expired.
216+
212217
---
213218

214219
## Tips

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "aldenn",
33
"license": "ISC",
44
"name": "react-webpack-typescript-starter",
5-
"version": "0.0.3",
5+
"version": "0.0.4",
66
"description": "The React-Typescript Boilerplate with Webpack config manual",
77
"main": "index.js",
88
"scripts": {

Diff for: src/components/hoc/withAuth.tsx

-23
This file was deleted.

Diff for: src/layouts/Auth.tsx

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, { useCallback, useEffect, useState } from 'react';
2+
import { useDispatch } from 'react-redux';
3+
import { setAuthenticated, setCurrentUser } from '@/store/slices/authSlice';
4+
import { getCurrentUser } from '@/apis/auth';
5+
6+
import { getToken } from '@/helpers/local-storage';
7+
import { goURL } from '@/helpers/router';
8+
9+
interface IProps {
10+
children: React.ReactElement;
11+
}
12+
13+
const Auth: React.FC<IProps> = ({ children }) => {
14+
const [renderRoute, setRenderRoute] = useState(false);
15+
const dispatch = useDispatch();
16+
17+
const fetchCurrentUser = useCallback(async () => {
18+
try {
19+
const response = await getCurrentUser();
20+
if (response && response.data) {
21+
dispatch(setAuthenticated(true));
22+
dispatch(setCurrentUser(response.data));
23+
}
24+
} catch (error) {
25+
goURL('/login');
26+
}
27+
setRenderRoute(true);
28+
}, [dispatch]);
29+
30+
useEffect(() => {
31+
if (!getToken()) {
32+
goURL('/login');
33+
setRenderRoute(true);
34+
} else {
35+
fetchCurrentUser();
36+
}
37+
// eslint-disable-next-line react-hooks/exhaustive-deps
38+
}, []);
39+
40+
return renderRoute ? children : null;
41+
};
42+
43+
export default Auth;

Diff for: src/layouts/main/index.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ const Main: React.FC = () => {
4444
path={path}
4545
render={props => {
4646
updateDisplayLayout(currentLayout, layout);
47+
/**
48+
* Use this for authentication like admin page
49+
*/
50+
// return (
51+
// <Auth>
52+
// <Component {...props} />
53+
// </Auth>
54+
// );
4755
return <Component {...props} />;
4856
}}
4957
{...rest}

Diff for: src/store/actions/auth.ts

-31
This file was deleted.

0 commit comments

Comments
 (0)