change to cookie

This commit is contained in:
Jonathan Baecker 2020-04-21 15:28:18 +02:00
parent 30a5309129
commit 85030c1843

View File

@ -2,25 +2,23 @@
import jwt_decode from 'jwt-decode' import jwt_decode from 'jwt-decode'
export const state = () => ({ export const state = () => ({
jwtToken: localStorage.getItem('token'), jwtToken: '',
jwtRefresh: localStorage.getItem('refresh'), jwtRefresh: '',
isLogin: false isLogin: false
}) })
// mutate values in state // mutate values in state
export const mutations = { export const mutations = {
UPADTE_TOKEN (state, obj) { UPADTE_TOKEN (state, obj) {
localStorage.setItem('token', obj.token)
state.jwtToken = obj.token state.jwtToken = obj.token
if (obj.refresh) { if (obj.refresh) {
localStorage.setItem('refresh', obj.refresh)
state.jwtRefresh = obj.refresh state.jwtRefresh = obj.refresh
} }
}, },
REMOVE_TOKEN (state) { REMOVE_TOKEN (state) {
localStorage.removeItem('token') this.$cookies.remove('token')
localStorage.removeItem('refresh') this.$cookies.remove('refresh')
state.jwtToken = null state.jwtToken = null
state.jwtRefresh = null state.jwtRefresh = null
}, },
@ -39,6 +37,14 @@ export const actions = {
.then((response) => { .then((response) => {
commit('UPADTE_TOKEN', { token: response.data.access, refresh: response.data.refresh }) commit('UPADTE_TOKEN', { token: response.data.access, refresh: response.data.refresh })
commit('UPDATE_IS_LOGIN', true) commit('UPDATE_IS_LOGIN', true)
this.$cookies.set('token', response.data.access, {
path: '/',
maxAge: 60 * 60 * 24 * 365
})
this.$cookies.set('refresh', response.data.refresh, {
path: '/',
maxAge: 60 * 60 * 24 * 365
})
}) })
.catch((error) => { .catch((error) => {
console.log(error) console.log(error)
@ -56,9 +62,11 @@ export const actions = {
}, },
async inspectToken ({ commit, dispatch, state }) { async inspectToken ({ commit, dispatch, state }) {
const token = state.jwtToken const token = this.$cookies.get('token')
const refresh = state.jwtRefresh const refresh = this.$cookies.get('refresh')
if (token && refresh) { if (token && refresh) {
commit('UPADTE_TOKEN', { token, refresh })
const decoded_token = jwt_decode(token) const decoded_token = jwt_decode(token)
const decoded_refresh = jwt_decode(refresh) const decoded_refresh = jwt_decode(refresh)
const timestamp = Date.now() / 1000 const timestamp = Date.now() / 1000