feature: move logout in main component

This commit is contained in:
Aleksei Tsybin 2022-03-02 23:30:13 +03:00
parent 678ba6ccd2
commit f80a836d9e
3 changed files with 2 additions and 13 deletions

View File

@ -1 +1 @@
<template> <div id="app"> <div class="container"> <HeaderComponent @checkAuth="checkAuth" :auth="isAuth" :user="user" /> <router-view @checkAuth="checkAuth" :user="user"></router-view> </div> </div> </template> <script> import HeaderComponent from '@/components/HeaderComponent' export default { components: { HeaderComponent }, data() { return { isAuth: null, user: '', } }, methods: { loggedOut() { this.isAuth = false }, async checkAuth() { this.isAuth = localStorage.getItem('jwt') != null if (this.isAuth) { await this.$http .get('me', { headers: { Authorization: `Bearer ${localStorage.getItem('jwt')}` }, }) .then((response) => { localStorage.setItem('user', JSON.stringify(response.data.user)) this.user = response.data.user }) .catch(function (error) { console.error(error.response) }) } }, }, async mounted() { await this.checkAuth() }, } </script>
<template> <div id="app"> <div class="container"> <HeaderComponent @loggedOut="loggedOut" :auth="isAuth" :user="user" /> <router-view @checkAuth="checkAuth" :user="user"></router-view> </div> </div> </template> <script> import HeaderComponent from '@/components/HeaderComponent' export default { components: { HeaderComponent }, data() { return { isAuth: null, user: '', } }, methods: { loggedOut() { localStorage.removeItem('jwt') localStorage.removeItem('user') if (localStorage.getItem('jwt') == null) { this.checkAuth() this.$router.push('login') } }, async checkAuth() { this.isAuth = localStorage.getItem('jwt') != null if (this.isAuth) { await this.$http .get('me', { headers: { Authorization: `Bearer ${localStorage.getItem('jwt')}` }, }) .then((response) => { localStorage.setItem('user', JSON.stringify(response.data.user)) this.user = response.data.user }) .catch(function (error) { console.error(error.response) }) } }, }, async mounted() { await this.checkAuth() }, } </script>

View File

@ -51,7 +51,7 @@
<b-button
v-if="auth"
class="button is-light is-rounded"
@click="loggedOut"
@click="$emit('loggedOut')"
>
Log out
</b-button>
@ -80,16 +80,6 @@ export default {
data() {
return {}
},
methods: {
loggedOut() {
localStorage.removeItem('jwt')
localStorage.removeItem('user')
if (localStorage.getItem('jwt') == null) {
this.$emit('checkAuth')
this.$router.push('login')
}
},
},
}
</script>
<style lang="scss" scoped>

View File

@ -82,7 +82,6 @@ export default {
.then((response) => {
localStorage.setItem('jwt', response.data.jwt_token)
if (localStorage.getItem('jwt') != null) {
// this.$emit('loggedIn')
this.$emit('checkAuth')
if (this.$route.query.nextUrl != null) {
this.$router.push({ path: `${this.$route.query.nextUrl}` })