Skip to content

Commit

Permalink
#176 [Frontend] Student Full Authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
giabao1511 committed Mar 5, 2023
2 parents 642601b + ebe047f commit 268b41c
Show file tree
Hide file tree
Showing 33 changed files with 491 additions and 102 deletions.
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@
"[sql]": {
"editor.defaultFormatter": "adpyke.vscode-sql-formatter"
},
"cSpell.words": ["argothim", "MIDDLAWARE", "Middlawre", "mutilp"]
"cSpell.words": [
"argothim",
"Localstorage",
"MIDDLAWARE",
"Middlawre",
"mutilp"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ paths:
#!created_at 28/02/2023
#!description: New Token
/api/v1/admin/renew-token:
post:
get:
summary: New Token
tags: [New Token]
operationId: reNewToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const adminController = {
domain:
CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT
? req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0]
: '',
: CONSTANTS.HEADER_DOMAIN,
maxAge: CONSTANTS._1_MONTH,
});

Expand Down Expand Up @@ -260,7 +260,10 @@ const adminController = {
httpOnly: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false,
sameSite: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false,
secure: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false,
domain: req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] || '',
domain:
CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT
? req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0]
: CONSTANTS.HEADER_DOMAIN,
maxAge: CONSTANTS._1_MONTH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ router.post('/login', adminController.LoginAdmin);
* @author Nguyễn Tiến Tài
* @created_at 28/02/2023
* @description Route renew-token token
* @param {('POST')} [method='POST'] The request's method
* @param {('GET')} [method='GET'] The request's method
*/
router.post('/renew-token', adminController.reNewToken);
router.get('/renew-token', adminController.reNewToken);

module.exports = router;
1 change: 1 addition & 0 deletions backend-manager-student/src/share/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ module.exports = {
HEADER_HEADER_FORWARDED_HOST: 'x-forwarded-host',
HEADER_FORWARDED_FOR: 'x-forwarded-for',
HEADER_DEVICE: 'user-agent',
HEADER_DOMAIN: 'localhost',
/**
* @author Nguyễn Tiến Tài
* @created_at 05/02/2023
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ const accessAdminMiddleware = async (req, res, next) => {

// Continue
return next();

} catch (error) {
return res.status(503).json({
status: 503,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ paths:
#!updated_at 08/02/2023
#!description: New Token
/api/v1/user/renew-token:
post:
get:
summary: New Token
tags: [New Token]
operationId: reNewToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ const userController = {
httpOnly: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false,
sameSite: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false,
secure: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false,
domain: req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] || '',
domain:
CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT
? req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0]
: CONSTANTS.HEADER_DOMAIN,
maxAge: CONSTANTS._1_MONTH,
});

Expand Down Expand Up @@ -388,7 +391,10 @@ const userController = {
httpOnly: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false,
sameSite: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false,
secure: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false,
domain: req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] || '',
domain:
CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT
? req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0]
: CONSTANTS.HEADER_DOMAIN,
maxAge: CONSTANTS._1_MONTH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ router.post('/login', userController.loginStudent);
* @author Nguyễn Tiến Tài
* @created_at 04/02/2023
* @description Route renew-token token
* @param {('POST')} [method='POST'] The request's method
* @param {('GET')} [method='GET'] The request's method
*/
router.post('/renew-token', userController.reNewToken);
router.get('/renew-token', userController.reNewToken);

/**
* @author Nguyễn Tiến Tài
Expand Down
9 changes: 9 additions & 0 deletions frontend-manager-cms/src/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,14 @@ const CONSTANTS = {
* @return {Number}
*/
AUTH_TOKEN: 'auth-token',
/**
* @author Nguyễn Tiến Tài
* @created_at 04/03/2023
* @descriptionKey Header
* @return {Number}
*/
OS_TYPE_HEADER: 'web',
OS_VERSION_HEADER: '1.0',
APP_VERSION_HEADER: '1.0',
};
export default CONSTANTS;
10 changes: 6 additions & 4 deletions frontend-manager-cms/src/utils/helper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! SHARE
import CONSTANTS from 'configs/constants';
import { getDeviceId, getToken } from './auth';

const HELPERS = {
/**
* @author Nguyễn Tiến Tài
* @created_at 02/03/2023
* @uodated_at 04/03/2023
* @descriptionKey return header browser
* @function getToken
* @return {String}
Expand All @@ -13,14 +15,14 @@ const HELPERS = {
// add the authorization to the headers
const headers = {
'X-DEVICE-ID': getDeviceId(),
'X-OS-TYPE': 'web',
'X-OS-VERSION': '1.0',
'X-APP-VERSION': '1.0',
'X-OS-TYPE': CONSTANTS.OS_TYPE_HEADER,
'X-OS-VERSION': CONSTANTS.OS_VERSION_HEADER,
'X-APP-VERSION': CONSTANTS.APP_VERSION_HEADER,
'X-DEVICE-NAME': window.navigator.userAgent,
};
const token = getToken();
if (token) {
headers.authorization = token ? `Bearer ${token}` : null;
headers.Authorization = token ? `Bearer ${token}` : null;
}

return headers;
Expand Down
7 changes: 7 additions & 0 deletions frontend-manager-student/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Run Frontend dev
run-dev:
npm run dev

# Run Production
run-product:
npm start
11 changes: 11 additions & 0 deletions frontend-manager-student/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend-manager-student/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"axios": "^0.25.0",
"bootstrap": "^5.2.3",
"framer-motion": "^6.2.8",
"jwt-decode": "^3.1.2",
"moment": "^2.29.1",
"nanoid": "^4.0.1",
"react": "^17.0.2",
Expand Down Expand Up @@ -50,6 +51,7 @@
},
"scripts": {
"start": "react-scripts start",
"dev": "set PORT=3000 && react-scripts start",
"build": "set \"GENERATE_SOURCEMAP=false\" && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
Expand Down
12 changes: 12 additions & 0 deletions frontend-manager-student/src/api/api_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,17 @@ const API_STUDENT = {
* @descriptionKey API login student
*/
LOGIN_STUDENT: '/student/v1/user/login',
/**
* @author Nguyễn Tiến Tài
* @created_at 03/03/2023
* @descriptionKey API profile student
*/
PROFILE_STUDENT: '/student/v1/user/private/profile',
/**
* @author Nguyễn Tiến Tài
* @created_at 04/03/2023
* @descriptionKey API new token student
*/
RE_NEW_TOKEN_STUDENT: '/student/v1/user/renew-token',
};
export default API_STUDENT;
67 changes: 17 additions & 50 deletions frontend-manager-student/src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,16 @@
import { useEffect, useRef, useState } from 'react';
//! LIBRARY
import React, { Fragment, useRef, useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { Link, useLocation } from 'react-router-dom';

//! SHARE
import { navInfo } from 'utils/dummy';

//! IMPORT
import { SCHOOL_LOGO } from '../imports/home_import/index';
import Button from './Button';

const navInfo = [
{
displayText: 'Giới thiệu',
path: '/',
submenu: [
{
displayText: 'Submenu 1',
path: '/sub',
},
{
displayText: 'Submenu 1',
path: '/sub',
},
{
displayText: 'Submenu 1',
path: '/sub',
},
],
},
{
displayText: 'Tra cứu',
path: '/book',
submenu: [
{
displayText: 'Thể loại',
path: '/category',
},
{
displayText: 'Tất cả tài liệu',
path: '/book',
},
{
displayText: 'Submenu 1',
path: '/sub',
},
],
},
{
displayText: 'Phụ kiện',
path: '/accessories',
},
{
displayText: 'Liên hệ',
path: '/contact',
},
];
//! COMPONENTS
import Button from './Button';

const userSubNav = [
{
Expand All @@ -68,6 +30,11 @@ const userSubNav = [
];

const Header = (props) => {
// Take profile account store
const { profile_student } = useSelector((state) => ({
...state.auth_student,
}));

const { pathname } = useLocation();
const headerRef = useRef(null);
const menuLeftRef = useRef(null);
Expand All @@ -88,7 +55,7 @@ const Header = (props) => {
};

return (
<>
<Fragment>
<div className="header" ref={headerRef}>
<div className="container">
<div className="header__logo">
Expand Down Expand Up @@ -188,7 +155,7 @@ const Header = (props) => {
</div>
</div>
</div>
</>
</Fragment>
);
};

Expand Down
10 changes: 10 additions & 0 deletions frontend-manager-student/src/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const CONSTANTS = {
*/
_1_MINUTES: 60 * 1000,
_5_MINUTES: 5 * 60 * 1000,
_4_MINUTES: 4 * 60 * 1000,
_15_MINUTES: 15 * 60 * 1000,
_45_MINUTES: 45 * 60 * 1000,
_1_DAY: 24 * 60 * 60 * 1000,
Expand Down Expand Up @@ -69,5 +70,14 @@ const CONSTANTS = {
* @return {Number}
*/
AUTH_TOKEN: 'auth-token',
/**
* @author Nguyễn Tiến Tài
* @created_at 04/03/2023
* @descriptionKey Header
* @return {Number}
*/
OS_TYPE_HEADER: 'web',
OS_VERSION_HEADER: '1.0',
APP_VERSION_HEADER: '1.0',
};
export default CONSTANTS;
9 changes: 9 additions & 0 deletions frontend-manager-student/src/configs/text_notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const TEXT_NOTIFICATION = {
/**
* @author Nguyễn Tiến Tài
* @created_at 04/03/2023
* @description LOgin success
*/
NOTIFICATION_LOGIN_SUCCESS: 'Login Student Success',
};
export default TEXT_NOTIFICATION;
31 changes: 31 additions & 0 deletions frontend-manager-student/src/contexts/auth_student/auth_student.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! LIBRARY
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';

//! SHARE
import HELPERS from 'utils/helper';
import CONSTANTS from '../../configs/constants';
import { getToken } from '../../utils/auth';

//! REDUX THUNK
import { Profile_Student_Initial } from '../../redux/student/authentication_slice/auth_thunk';

const AuthStudent = () => {
// InitialState Action
const dispatch = useDispatch();

// Get Token localStore
const token_localStorage = getToken(CONSTANTS.AUTH_TOKEN);

//Check Token
const decodedToken = HELPERS.isTokenExpired(token_localStorage);

useEffect(() => {
if (token_localStorage && decodedToken) {
dispatch(Profile_Student_Initial(token_localStorage));
}
}, []);

return {};
};
export default AuthStudent;
Loading

0 comments on commit 268b41c

Please sign in to comment.