Awesome, thanks for the advice. I'm a bit of a noob with Vue/Nuxt, maybe there's a cleaner way to do it, but something like this seems to work (setting user on login, clearing user on logout).
Authcontroller.php return response([ 'user' => $user->name, 'token' => $resp->access_token, 'expiresIn' => $resp->expires_in, 'message' => 'You have been logged in', ], 200);
Added the user in store/index.js export const state = () => ({ token: null, user: null, });
Added Vuex mutations: SET_USER(state, user) { state.user = user; } REMOVE_USER(state, user) { state.user = null; },
Added Vuex actions: setUser({commit}, user) { commit('SET_USER', user); }, removeUser({commit}) { commit('REMOVE_USER'); }
Changed logout method logout: function() { this.$axios.post('/api/logout') .then(resp => { this.$store.dispatch('removeUser'); this.$store.dispatch('logout'); this.$router.push('/login'); }) .catch(errors => { console.log(errors); }); }
Login.vue, login() method: login() { this.$axios.$post('/api/login', this.form) .then(({token, expiresIn, user}) => { this.$store.dispatch('setToken', {token, expiresIn}); this.$store.dispatch('setUser', user); this.$router.push('/'); }) .catch(errors => { console.log(errors); }) }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Awesome, thanks for the advice. I'm a bit of a noob with Vue/Nuxt, maybe there's a cleaner way to do it, but something like this seems to work (setting user on login, clearing user on logout).
Authcontroller.php
return response([
'user' => $user->name,
'token' => $resp->access_token,
'expiresIn' => $resp->expires_in,
'message' => 'You have been logged in',
], 200);
Added the user in store/index.js
export const state = () => ({
token: null,
user: null,
});
Added Vuex mutations:
SET_USER(state, user) {
state.user = user;
}
REMOVE_USER(state, user) {
state.user = null;
},
Added Vuex actions:
setUser({commit}, user) {
commit('SET_USER', user);
},
removeUser({commit}) {
commit('REMOVE_USER');
}
Changed logout method
logout: function() {
this.$axios.post('/api/logout')
.then(resp => {
this.$store.dispatch('removeUser');
this.$store.dispatch('logout');
this.$router.push('/login');
})
.catch(errors => {
console.log(errors);
});
}
Login.vue, login() method:
login() {
this.$axios.$post('/api/login', this.form)
.then(({token, expiresIn, user}) => {
this.$store.dispatch('setToken', {token, expiresIn});
this.$store.dispatch('setUser', user);
this.$router.push('/');
})
.catch(errors => {
console.log(errors);
})
}