Browse Source

Used vuex and added username in list

pull/664/head
tusharmakkar08 4 years ago
parent
commit
13b717a5cd
2 changed files with 19 additions and 12 deletions
  1. 18
      frontend/components/organisms/layout/TheHeader.vue
  2. 13
      frontend/store/auth.js

18
frontend/components/organisms/layout/TheHeader.vue

@ -59,15 +59,15 @@
</v-btn>
<v-menu
v-if="isAuthenticated"
bottom
offset-y
>
<template v-slot:activator="{ on }">
<v-btn v-on="on" outlined>
{{ username }}
<v-btn v-on="on" on icon>
<v-icon>mdi-dots-vertical</v-icon>
</v-btn>
</template>
<v-list dense>
<v-list>
<v-subheader>{{ username }}</v-subheader>
<v-list-item @click="signout">
<v-list-item-icon>
<v-icon>mdi-logout</v-icon>
@ -84,7 +84,7 @@
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
import { mapGetters, mapActions, mapState } from 'vuex'
import TheColorModeSwitcher from '@/components/organisms/layout/TheColorModeSwitcher'
export default {
@ -99,13 +99,13 @@ export default {
{ title: 'Sentiment Analysis', link: 'sentiment-analysis' },
{ title: 'Translation', link: 'translation' },
{ title: 'Text to SQL', link: 'text-to-sql' }
],
username: localStorage.getItem('name')
]
}
},
computed: {
...mapGetters('auth', ['isAuthenticated'])
...mapGetters('auth', ['isAuthenticated']),
...mapState('auth', ['username'])
},
methods: {

13
frontend/store/auth.js

@ -3,7 +3,8 @@ import ApiService from '@/services/api.service'
import AuthService from '@/services/auth.service'
export const state = () => ({
token: null
token: null,
username: null
})
export const mutations = {
@ -12,6 +13,12 @@ export const mutations = {
},
clearToken(state) {
state.token = null
},
setUsername(state, username) {
state.username = username
},
clearUsername(state) {
state.username = null
}
}
@ -26,8 +33,8 @@ export const actions = {
return AuthService.postCredential(authData)
.then((result) => {
commit('setToken', result.data.token)
commit('setUsername', authData.username)
localStorage.setItem('token', result.data.token)
localStorage.setItem('name', authData.username)
Cookie.set('jwt', result.data.token)
ApiService.setHeader(result.data.token)
})
@ -53,10 +60,10 @@ export const actions = {
},
logout({ commit }) {
commit('clearToken')
commit('clearUsername')
Cookie.remove('jwt')
if (process.client) {
localStorage.removeItem('token')
localStorage.removeItem('name')
}
}
}
Loading…
Cancel
Save