diff --git a/client/src/utils/checkAuthenticationAdmin.ts b/client/src/utils/checkAuthenticationAdmin.ts index ce657e2f9d1b98134c8b79ea69f2476527cedcbd..81ebcb3a753272e9bb377c5ef2294ecd6f90acd8 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 06194dcfb743e961afd64f18bde249cd492508ff..a9a2f09cbec4ed13f2bc850db9c43c74f24d6461 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 } } }