Created
June 18, 2020 10:51
-
-
Save pipethedev/6793d31bcae6a132dc50d6669b187da3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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