mirror of
https://github.com/ZorgCC/psina-avatar-frontend.git
synced 2025-06-02 16:47:32 +03:00
feature: move logout in main component
This commit is contained in:
parent
678ba6ccd2
commit
f80a836d9e
@ -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>
|
@ -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>
|
||||
|
@ -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}` })
|
||||
|
Loading…
x
Reference in New Issue
Block a user