From 7459544f4839f81798a4003b8365dac1a76fb881 Mon Sep 17 00:00:00 2001 From: bmodee <bjomo323@student.liu.se> Date: Tue, 25 May 2021 10:40:59 +0200 Subject: [PATCH] Add comments --- client/src/utils/checkAuthenticationAdmin.ts | 16 ++++++++++------ .../src/utils/checkAuthenticationCompetition.ts | 15 ++++++++++----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/client/src/utils/checkAuthenticationAdmin.ts b/client/src/utils/checkAuthenticationAdmin.ts index ce657e2f..81ebcb3a 100644 --- a/client/src/utils/checkAuthenticationAdmin.ts +++ b/client/src/utils/checkAuthenticationAdmin.ts @@ -6,32 +6,36 @@ import Types from '../actions/types' import { logoutUser } from '../actions/user' import store from '../store' +/** The user is not authorized => logout the user*/ const UnAuthorized = async () => { await logoutUser()(store.dispatch) } export const CheckAuthenticationAdmin = async () => { - const authToken = localStorage.token + const authToken = localStorage.token // Retrives from local storage if (authToken) { - const decodedToken: any = jwtDecode(authToken) + // If the user has an authtoken + const decodedToken: any = jwtDecode(authToken) // Decode it if (decodedToken.exp * 1000 >= Date.now()) { + // Check expiration data anb if it is still valid axios.defaults.headers.common['Authorization'] = authToken store.dispatch({ type: Types.LOADING_USER }) await axios .get('/api/users') .then((res) => { - store.dispatch({ type: Types.SET_AUTHENTICATED }) + store.dispatch({ type: Types.SET_AUTHENTICATED }) // Make user authenticated store.dispatch({ type: Types.SET_USER, payload: res.data, }) }) .catch((error) => { - console.log(error) - UnAuthorized() + // An error has occured + console.log(error) // Log the error + UnAuthorized() // The user is not authorized }) } else { - await UnAuthorized() + await UnAuthorized() // The user is not authorized } } } diff --git a/client/src/utils/checkAuthenticationCompetition.ts b/client/src/utils/checkAuthenticationCompetition.ts index 06194dcf..a9a2f09c 100644 --- a/client/src/utils/checkAuthenticationCompetition.ts +++ b/client/src/utils/checkAuthenticationCompetition.ts @@ -9,19 +9,23 @@ import { getPresentationCompetition, setPresentationCode } from '../actions/pres import Types from '../actions/types' import store from '../store' +/** The user is not authorized => logout the user*/ const UnAuthorized = async (role: 'Judge' | 'Operator' | 'Team' | 'Audience') => { await logoutCompetition(role)(store.dispatch) } export const CheckAuthenticationCompetition = async (role: 'Judge' | 'Operator' | 'Team' | 'Audience') => { - const authToken = localStorage[`${role}Token`] + const authToken = localStorage[`${role}Token`] // Retrives from local storage if (authToken) { - const decodedToken: any = jwtDecode(authToken) + // If the user has an authtoken + const decodedToken: any = jwtDecode(authToken) // Decode it + // Check expiration data anb if it is still valid if (decodedToken.exp * 1000 >= Date.now()) { axios.defaults.headers.common['Authorization'] = authToken await axios .get('/api/auth/test') .then(() => { + // Make user authenticated store.dispatch({ type: Types.SET_COMPETITION_LOGIN_DATA, payload: { @@ -34,11 +38,12 @@ export const CheckAuthenticationCompetition = async (role: 'Judge' | 'Operator' setPresentationCode(decodedToken.code)(store.dispatch) }) .catch((error) => { - console.log(error) - UnAuthorized(role) + // An error has occured + console.log(error) // Log the error + UnAuthorized(role) // The user is not authorized }) } else { - await UnAuthorized(role) + await UnAuthorized(role) // The user is not authorized } } } -- GitLab