Skip to content

Instantly share code, notes, and snippets.

@pipethedev
Created June 18, 2020 10:51
Show Gist options
  • Select an option

  • Save pipethedev/6793d31bcae6a132dc50d6669b187da3 to your computer and use it in GitHub Desktop.

Select an option

Save pipethedev/6793d31bcae6a132dc50d6669b187da3 to your computer and use it in GitHub Desktop.
import axios from 'axios'
export default{
namespaced: true,
state: {
token: null,
user : null
},
getters:{
authenticated(state){
return state.token && state.user
},
user(state){
return state.user
}
},
mutations: {
SET_TOKEN (state, token){
state.token = token
},
SET_USER (state, data){
state.user = data
}
},
actions: {
async signIn({ dispatch }, credentials){
let response = await axios.post('auth/signin', credentials)
return dispatch('attempt', response.data.token)
},
async attempt({ commit, state }, token){
if(token){
commit('SET_TOKEN', token)
}
if(!state.token){
return
}
try{
let response = await axios.get('auth/me')
commit('SET_USER', response.data)
}catch(e){
commit('SET_TOKEN', null)
commit('SET_USER', null)
}
}
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment