2020-01-29 11:40:15 -05:00
|
|
|
/* eslint-disable camelcase */
|
|
|
|
import jwt_decode from 'jwt-decode'
|
|
|
|
|
|
|
|
export const state = () => ({
|
2020-04-21 09:28:18 -04:00
|
|
|
jwtToken: '',
|
|
|
|
jwtRefresh: '',
|
2020-01-29 11:40:15 -05:00
|
|
|
isLogin: false
|
|
|
|
})
|
|
|
|
|
|
|
|
// mutate values in state
|
|
|
|
export const mutations = {
|
|
|
|
UPADTE_TOKEN (state, obj) {
|
|
|
|
state.jwtToken = obj.token
|
2020-05-25 16:37:44 -04:00
|
|
|
this.$cookies.set('token', obj.token, {
|
|
|
|
path: '/',
|
|
|
|
maxAge: 60 * 60 * 24 * 365,
|
|
|
|
sameSite: 'lax'
|
|
|
|
})
|
2020-01-29 11:40:15 -05:00
|
|
|
|
|
|
|
if (obj.refresh) {
|
|
|
|
state.jwtRefresh = obj.refresh
|
2020-05-25 16:37:44 -04:00
|
|
|
this.$cookies.set('refresh', obj.refresh, {
|
|
|
|
path: '/',
|
|
|
|
maxAge: 60 * 60 * 24 * 365,
|
|
|
|
sameSite: 'lax'
|
|
|
|
})
|
2020-01-29 11:40:15 -05:00
|
|
|
}
|
|
|
|
},
|
2020-05-25 16:37:44 -04:00
|
|
|
UPDATE_IS_LOGIN (state, bool) {
|
|
|
|
state.isLogin = bool
|
|
|
|
},
|
2020-01-29 11:40:15 -05:00
|
|
|
REMOVE_TOKEN (state) {
|
2020-04-21 09:28:18 -04:00
|
|
|
this.$cookies.remove('token')
|
|
|
|
this.$cookies.remove('refresh')
|
2020-01-29 11:40:15 -05:00
|
|
|
state.jwtToken = null
|
|
|
|
state.jwtRefresh = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
async obtainToken ({ commit, state }, { username, password }) {
|
|
|
|
const payload = {
|
|
|
|
username,
|
|
|
|
password
|
|
|
|
}
|
2020-05-12 12:07:53 -04:00
|
|
|
let code = null
|
2020-01-29 11:40:15 -05:00
|
|
|
await this.$axios.post('auth/token/', payload)
|
|
|
|
.then((response) => {
|
|
|
|
commit('UPADTE_TOKEN', { token: response.data.access, refresh: response.data.refresh })
|
|
|
|
commit('UPDATE_IS_LOGIN', true)
|
2020-05-12 12:07:53 -04:00
|
|
|
code = response.status
|
2020-01-29 11:40:15 -05:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2020-05-12 12:07:53 -04:00
|
|
|
code = error.response.status
|
2020-01-29 11:40:15 -05:00
|
|
|
})
|
2020-05-12 12:07:53 -04:00
|
|
|
|
|
|
|
return code
|
2020-01-29 11:40:15 -05:00
|
|
|
},
|
|
|
|
async refreshToken ({ commit, state }) {
|
|
|
|
const payload = {
|
2020-07-06 08:42:58 -04:00
|
|
|
refresh: state.jwtRefresh,
|
|
|
|
progress: false
|
2020-01-29 11:40:15 -05:00
|
|
|
}
|
|
|
|
|
2020-05-24 07:35:22 -04:00
|
|
|
await this.$axios.post('auth/token/refresh/', payload)
|
|
|
|
.then((response) => {
|
|
|
|
commit('UPADTE_TOKEN', { token: response.data.access })
|
|
|
|
commit('UPDATE_IS_LOGIN', true)
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
if (error.response.status === 401) {
|
2020-05-24 11:51:06 -04:00
|
|
|
commit('REMOVE_TOKEN')
|
2020-05-24 07:35:22 -04:00
|
|
|
commit('UPDATE_IS_LOGIN', false)
|
|
|
|
}
|
|
|
|
})
|
2020-01-29 11:40:15 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
async inspectToken ({ commit, dispatch, state }) {
|
2020-04-21 09:28:18 -04:00
|
|
|
const token = this.$cookies.get('token')
|
|
|
|
const refresh = this.$cookies.get('refresh')
|
|
|
|
|
2020-01-29 11:40:15 -05:00
|
|
|
if (token && refresh) {
|
2020-04-21 09:28:18 -04:00
|
|
|
commit('UPADTE_TOKEN', { token, refresh })
|
2020-01-29 11:40:15 -05:00
|
|
|
const decoded_token = jwt_decode(token)
|
|
|
|
const decoded_refresh = jwt_decode(refresh)
|
|
|
|
const timestamp = Date.now() / 1000
|
|
|
|
const expire_token = decoded_token.exp
|
|
|
|
const expire_refresh = decoded_refresh.exp
|
2020-05-25 16:37:44 -04:00
|
|
|
|
2020-09-15 04:27:21 -04:00
|
|
|
if (state.jwtToken && expire_token - timestamp > 15) {
|
2020-01-29 11:40:15 -05:00
|
|
|
// DO NOTHING, DO NOT REFRESH
|
|
|
|
commit('UPDATE_IS_LOGIN', true)
|
2020-09-15 04:27:21 -04:00
|
|
|
} else if (!state.jwtToken || expire_refresh - timestamp > 0) {
|
|
|
|
commit('UPADTE_TOKEN', { token, refresh })
|
2020-01-29 11:40:15 -05:00
|
|
|
await dispatch('refreshToken')
|
|
|
|
} else {
|
|
|
|
// PROMPT USER TO RE-LOGIN, THIS ELSE CLAUSE COVERS THE CONDITION WHERE A TOKEN IS EXPIRED AS WELL
|
|
|
|
commit('UPDATE_IS_LOGIN', false)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
commit('UPDATE_IS_LOGIN', false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|