Created
October 4, 2020 03:13
-
-
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
This file contains 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
<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