Client - add logout

This commit is contained in:
Sam 2021-08-14 18:58:59 +02:00
parent 1041fc4cb8
commit 5ade82d61d
6 changed files with 38 additions and 1 deletions

View File

@ -43,7 +43,9 @@
</div> </div>
</div> </div>
<div class="nav-item">{{ authUser.username }}</div> <div class="nav-item">{{ authUser.username }}</div>
<div class="nav-item">{{ t('user.LOGOUT') }}</div> <div class="nav-item nav-link" @click="logout">
{{ t('user.LOGOUT') }}
</div>
</div> </div>
<div class="nav-items-group" v-else> <div class="nav-items-group" v-else>
<span class="nav-item">{{ t('user.REGISTER') }}</span> <span class="nav-item">{{ t('user.REGISTER') }}</span>
@ -117,6 +119,9 @@
locale.value = option.value.toString() locale.value = option.value.toString()
store.commit(ROOT_STORE.MUTATIONS.UPDATE_LANG, option.value) store.commit(ROOT_STORE.MUTATIONS.UPDATE_LANG, option.value)
} }
function logout() {
store.dispatch(USER_STORE.ACTIONS.LOGOUT)
}
return { return {
availableLanguages, availableLanguages,
@ -129,6 +134,7 @@
openMenu, openMenu,
closeMenu, closeMenu,
updateLanguage, updateLanguage,
logout,
} }
}, },
}) })
@ -218,6 +224,11 @@
} }
} }
.nav-link {
color: var(--app-a-color);
cursor: pointer;
}
.nav-profile-img { .nav-profile-img {
border-radius: 50%; border-radius: 50%;
height: 32px; height: 32px;

View File

@ -52,4 +52,12 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
}) })
.catch((error) => handleError(context, error)) .catch((error) => handleError(context, error))
}, },
[USER_STORE.ACTIONS.LOGOUT](
context: ActionContext<IUserState, IRootState>
): void {
localStorage.removeItem('authToken')
context.commit(USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN)
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGE)
router.push('/login')
},
} }

View File

@ -1,6 +1,7 @@
export enum UserActions { export enum UserActions {
GET_USER_PROFILE = 'GET_USER_PROFILE', GET_USER_PROFILE = 'GET_USER_PROFILE',
LOGIN_OR_REGISTER = 'LOGIN_OR_REGISTER', LOGIN_OR_REGISTER = 'LOGIN_OR_REGISTER',
LOGOUT = 'LOGOUT',
} }
export enum UserGetters { export enum UserGetters {
@ -10,6 +11,7 @@ export enum UserGetters {
} }
export enum UserMutations { export enum UserMutations {
CLEAR_AUTH_USER_TOKEN = 'CLEAR_AUTH_USER_TOKEN',
UPDATE_AUTH_TOKEN = 'UPDATE_AUTH_TOKEN', UPDATE_AUTH_TOKEN = 'UPDATE_AUTH_TOKEN',
UPDATE_AUTH_USER_PROFILE = 'UPDATE_AUTH_USER_PROFILE', UPDATE_AUTH_USER_PROFILE = 'UPDATE_AUTH_USER_PROFILE',
} }

View File

@ -38,10 +38,16 @@ export interface IUserState {
} }
export interface IUserActions { export interface IUserActions {
[USER_STORE.ACTIONS.GET_USER_PROFILE](
context: ActionContext<IUserState, IRootState>
): void
[USER_STORE.ACTIONS.LOGIN_OR_REGISTER]( [USER_STORE.ACTIONS.LOGIN_OR_REGISTER](
context: ActionContext<IUserState, IRootState>, context: ActionContext<IUserState, IRootState>,
data: ILoginOrRegisterData data: ILoginOrRegisterData
): void ): void
[USER_STORE.ACTIONS.LOGOUT](
context: ActionContext<IUserState, IRootState>
): void
} }
export interface IUserGetters { export interface IUserGetters {

View File

@ -5,6 +5,10 @@ import { IAuthUserProfile, IUserState } from '@/store/modules/user/interfaces'
import { TUserMutations } from '@/store/modules/user/types' import { TUserMutations } from '@/store/modules/user/types'
export const mutations: MutationTree<IUserState> & TUserMutations = { export const mutations: MutationTree<IUserState> & TUserMutations = {
[USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN](state: IUserState) {
state.authToken = null
state.authUserProfile = <IAuthUserProfile>{}
},
[USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN]( [USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN](
state: IUserState, state: IUserState,
authToken: string authToken: string

View File

@ -2,13 +2,19 @@ import { Store as VuexStore, CommitOptions, DispatchOptions } from 'vuex'
import { USER_STORE } from '@/store/constants' import { USER_STORE } from '@/store/constants'
import { import {
IAuthUserProfile,
IUserActions, IUserActions,
IUserGetters, IUserGetters,
IUserState, IUserState,
} from '@/store/modules/user/interfaces' } from '@/store/modules/user/interfaces'
export type TUserMutations<S = IUserState> = { export type TUserMutations<S = IUserState> = {
[USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN](state: S): void
[USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN](state: S, authToken: string): void [USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN](state: S, authToken: string): void
[USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE](
state: S,
authUserProfile: IAuthUserProfile
): void
} }
export type TUserStoreModule<S = IUserState> = Omit< export type TUserStoreModule<S = IUserState> = Omit<