Skip to content
Snippets Groups Projects
Commit 7459544f authored by Björn Modée's avatar Björn Modée
Browse files

Add comments

parent 619f15b9
No related branches found
No related tags found
No related merge requests found
Pipeline #46740 passed with warnings
......@@ -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
}
}
}
......@@ -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
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment