Client - add global navigation guards

This commit is contained in:
Sam
2021-08-14 19:24:19 +02:00
parent 5ade82d61d
commit a3ee5b69a7
4 changed files with 48 additions and 0 deletions

View File

@ -13,6 +13,20 @@ import api from '@/api/defaultApi'
import router from '@/router'
export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
[USER_STORE.ACTIONS.CHECK_AUTH_USER](
context: ActionContext<IUserState, IRootState>
): void {
if (
window.localStorage.authToken &&
!context.getters[USER_STORE.GETTERS.IS_AUTHENTICATED]
) {
context.commit(
USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN,
window.localStorage.authToken
)
context.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
}
},
[USER_STORE.ACTIONS.GET_USER_PROFILE](
context: ActionContext<IUserState, IRootState>
): void {

View File

@ -1,4 +1,5 @@
export enum UserActions {
CHECK_AUTH_USER = 'CHECK_AUTH_USER',
GET_USER_PROFILE = 'GET_USER_PROFILE',
LOGIN_OR_REGISTER = 'LOGIN_OR_REGISTER',
LOGOUT = 'LOGOUT',

View File

@ -38,6 +38,9 @@ export interface IUserState {
}
export interface IUserActions {
[USER_STORE.ACTIONS.CHECK_AUTH_USER](
context: ActionContext<IUserState, IRootState>
): void
[USER_STORE.ACTIONS.GET_USER_PROFILE](
context: ActionContext<IUserState, IRootState>
): void