Client - add global navigation guards
This commit is contained in:
parent
5ade82d61d
commit
a3ee5b69a7
@ -1,5 +1,7 @@
|
|||||||
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
|
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
|
||||||
|
|
||||||
|
import { USER_STORE } from '@/store/constants'
|
||||||
|
import store from '@/store'
|
||||||
import Dashboard from '@/views/DashBoard.vue'
|
import Dashboard from '@/views/DashBoard.vue'
|
||||||
import Login from '@/views/Login.vue'
|
import Login from '@/views/Login.vue'
|
||||||
import NotFound from '@/views/NotFound.vue'
|
import NotFound from '@/views/NotFound.vue'
|
||||||
@ -23,4 +25,32 @@ const router = createRouter({
|
|||||||
routes,
|
routes,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.beforeEach((to, from, next) => {
|
||||||
|
store
|
||||||
|
.dispatch(USER_STORE.ACTIONS.CHECK_AUTH_USER)
|
||||||
|
.then(() => {
|
||||||
|
if (
|
||||||
|
store.getters[USER_STORE.GETTERS.IS_AUTHENTICATED] &&
|
||||||
|
['/login'].includes(to.path)
|
||||||
|
) {
|
||||||
|
return next('/')
|
||||||
|
} else if (
|
||||||
|
!store.getters[USER_STORE.GETTERS.IS_AUTHENTICATED] &&
|
||||||
|
!['/login'].includes(to.path)
|
||||||
|
) {
|
||||||
|
const path =
|
||||||
|
to.path === '/'
|
||||||
|
? { path: '/login' }
|
||||||
|
: { path: '/login', query: { from: to.fullPath } }
|
||||||
|
next(path)
|
||||||
|
} else {
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(error)
|
||||||
|
next()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
@ -13,6 +13,20 @@ import api from '@/api/defaultApi'
|
|||||||
import router from '@/router'
|
import router from '@/router'
|
||||||
|
|
||||||
export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
|
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](
|
[USER_STORE.ACTIONS.GET_USER_PROFILE](
|
||||||
context: ActionContext<IUserState, IRootState>
|
context: ActionContext<IUserState, IRootState>
|
||||||
): void {
|
): void {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
export enum UserActions {
|
export enum UserActions {
|
||||||
|
CHECK_AUTH_USER = 'CHECK_AUTH_USER',
|
||||||
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',
|
LOGOUT = 'LOGOUT',
|
||||||
|
@ -38,6 +38,9 @@ export interface IUserState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface IUserActions {
|
export interface IUserActions {
|
||||||
|
[USER_STORE.ACTIONS.CHECK_AUTH_USER](
|
||||||
|
context: ActionContext<IUserState, IRootState>
|
||||||
|
): void
|
||||||
[USER_STORE.ACTIONS.GET_USER_PROFILE](
|
[USER_STORE.ACTIONS.GET_USER_PROFILE](
|
||||||
context: ActionContext<IUserState, IRootState>
|
context: ActionContext<IUserState, IRootState>
|
||||||
): void
|
): void
|
||||||
|
Loading…
Reference in New Issue
Block a user