Skip to content

Instantly share code, notes, and snippets.

@blogcacanid
Created October 4, 2020 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blogcacanid/efff63138d979c0164f5c40b4543acbe to your computer and use it in GitHub Desktop.
Save blogcacanid/efff63138d979c0164f5c40b4543acbe to your computer and use it in GitHub Desktop.
AppComponent.vue - Login System Dengan JSON Web Token Laravel 7 Dan Vue - 02 Front End
<template>
<router-view></router-view>
</template>
<script>
import store from '../store'
export default {
created() {
if(localStorage.token) {
axios.get('/api/auth/profile', {
headers: {
Authorization: 'Bearer ' + localStorage.getItem('token')
}
},
).then(response => {
store.commit('loginUser')
}).catch(error => {
if (error.response.status === 401 || error.response.status === 403) {
store.commit('logoutUser')
localStorage.setItem('token', '')
this.$router.push({name: 'login'})
}
});
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment