You can make a helper in vue
// Setting bearer token for the api calls
setUser(user) {
localStorage.removeItem('getUser');
localStorage.setItem('getUser', user); // store the token in localstorage
},// Setting bearer token for the api calls
setUserId(id) {
localStorage.removeItem('getUserId');
localStorage.setItem('getUserId', id); // store the token in localstorage
},
setRole(role) {
localStorage.removeItem('getRole');
localStorage.setItem('getRole', role); // store the token in localstorage
},
and You can use in a component when logged in the user
axios.post('/api/login', this.$data)
.then(res => {
this.setUser(res.data.data.access_token);
axios.defaults.headers.common['Authorization'] = localStorage.getItem('getUser')
this.setUserId(res.data.data.user.id);
this.setProfileImage(res.data.data.user.image);
this.setRole(res.data.data.role);
this.$router.push('/dashboard')
})
.catch(error => {
this.errors = error.response.data.msg;
})