Common "HeaderIntern" for user routes

This commit is contained in:
Jörn-Michael Miehe 2019-05-27 15:50:23 +02:00
parent 3b2ff65054
commit 1bc80b6d89
3 changed files with 116 additions and 54 deletions

View file

@ -45,7 +45,7 @@
<v-icon>edit</v-icon> <v-icon>edit</v-icon>
</v-btn> </v-btn>
<v-dialog v-model="deleting" persistent> <v-dialog v-model="deleting">
<template v-slot:activator="{ on }"> <template v-slot:activator="{ on }">
<v-btn fab absolute bottom left v-on="on"> <v-btn fab absolute bottom left v-on="on">
<v-icon>delete</v-icon> <v-icon>delete</v-icon>

View file

@ -0,0 +1,109 @@
<template>
<Header>
<v-btn flat :to="{ name: 'usercp' }"><v-icon>person</v-icon> {{ user.login }}</v-btn>
<v-dialog v-model="logging_out">
<template v-slot:activator="{ on }">
<v-btn flat v-on="on">
Logout
</v-btn>
</template>
<v-card>
<v-card-title class="headline">
Log Out?
</v-card-title>
<v-card-text>
Are you sure you want to log out?
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="error" @click.native="logging_out = false">
Cancel
</v-btn>
<v-btn
color="success"
@click.native="logout"
>
Confirm
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</Header>
</template>
<script>
import * as Cookies from 'js-cookie'
import axios from 'axios'
import Header from './Header'
export default {
name: 'HeaderIntern',
components: {
Header
},
data: () => ({
logging_out: false
}),
props: {
value: String
},
methods: {
goHome() {
this.$emit('input', '')
Cookies.remove('session')
this.$router.push({ name: 'home' })
},
logout() {
axios
.post('/user/logout', {
session: this.value
})
.then(response => {
if (response.data.success) {
this.goHome()
}
})
}
},
computed: {
session: () => Cookies.get('session')
},
asyncComputed: {
user: {
get() {
return axios
.post('/user/info', {
session: this.session
})
.then(response => {
if (response.data.success) {
this.$emit('input', this.session)
return response.data.user
} else {
this.goHome()
return null
}
})},
default: {
user: 0,
login: '',
settings: ''
}
}
}
}
</script>

View file

@ -1,13 +1,7 @@
<template> <template>
<v-content> <v-content>
<v-container> <v-container>
<Header> <HeaderIntern v-model="session" />
<v-btn flat @click.native="logout">Logout</v-btn>
</Header>
<template v-if="user">
<p>user logged in: {{ user.login }}</p>
</template>
<DeckList :session="session" /> <DeckList :session="session" />
</v-container> </v-container>
@ -15,60 +9,19 @@
</template> </template>
<script> <script>
import * as Cookies from 'js-cookie' import HeaderIntern from '@/components/HeaderIntern.vue'
import axios from '@/plugins/axios'
import Header from '@/components/Header.vue'
import DeckList from '@/components/DeckList.vue' import DeckList from '@/components/DeckList.vue'
export default { export default {
name: 'UserCP', name: 'UserCP',
components: { components: {
Header, HeaderIntern,
DeckList DeckList
}, },
data: () => ({}), data: () => ({
session: ''
methods: {
goHome() {
Cookies.remove('session')
this.$router.push({ name: 'home' })
},
logout() {
axios
.post('/user/logout', {
session: this.session
}) })
.then(response => {
if (response.data.success) {
this.goHome()
}
})
}
},
computed: {
session: () => Cookies.get('session')
},
asyncComputed: {
user() {
return axios
.post('/user/info', {
session: this.session
})
.then(response => {
if (response.data.success) {
return response.data.user
} else {
this.goHome()
return null
}
})
}
}
} }
</script> </script>