Client - refacto interceptors by using meta info

This commit is contained in:
Sam 2023-09-20 08:24:19 +02:00
parent efbf57ca46
commit 6e5717f8ba

View File

@ -53,6 +53,7 @@ const routes: Array<RouteRecordRaw> = [
props: { action: 'login' }, props: { action: 'login' },
meta: { meta: {
title: 'user.LOGIN', title: 'user.LOGIN',
withoutAuth: true,
}, },
}, },
{ {
@ -62,6 +63,7 @@ const routes: Array<RouteRecordRaw> = [
props: { action: 'register' }, props: { action: 'register' },
meta: { meta: {
title: 'user.REGISTER', title: 'user.REGISTER',
withoutAuth: true,
}, },
}, },
{ {
@ -73,6 +75,7 @@ const routes: Array<RouteRecordRaw> = [
), ),
meta: { meta: {
title: 'user.ACCOUNT_CONFIRMATION', title: 'user.ACCOUNT_CONFIRMATION',
withoutAuth: true,
}, },
}, },
{ {
@ -85,6 +88,7 @@ const routes: Array<RouteRecordRaw> = [
props: { action: 'account-confirmation-resend' }, props: { action: 'account-confirmation-resend' },
meta: { meta: {
title: 'buttons.ACCOUNT-CONFIRMATION-RESEND', title: 'buttons.ACCOUNT-CONFIRMATION-RESEND',
withoutAuth: true,
}, },
}, },
{ {
@ -97,6 +101,7 @@ const routes: Array<RouteRecordRaw> = [
props: { action: 'email-sent' }, props: { action: 'email-sent' },
meta: { meta: {
title: 'buttons.ACCOUNT-CONFIRMATION-RESEND', title: 'buttons.ACCOUNT-CONFIRMATION-RESEND',
withoutAuth: true,
}, },
}, },
{ {
@ -109,6 +114,7 @@ const routes: Array<RouteRecordRaw> = [
props: { action: 'request-sent' }, props: { action: 'request-sent' },
meta: { meta: {
title: 'user.PASSWORD_RESET', title: 'user.PASSWORD_RESET',
withoutAuth: true,
}, },
}, },
{ {
@ -121,6 +127,7 @@ const routes: Array<RouteRecordRaw> = [
props: { action: 'reset-request' }, props: { action: 'reset-request' },
meta: { meta: {
title: 'user.PASSWORD_RESET', title: 'user.PASSWORD_RESET',
withoutAuth: true,
}, },
}, },
{ {
@ -133,6 +140,7 @@ const routes: Array<RouteRecordRaw> = [
props: { action: 'password-updated' }, props: { action: 'password-updated' },
meta: { meta: {
title: 'user.PASSWORD_RESET', title: 'user.PASSWORD_RESET',
withoutAuth: true,
}, },
}, },
{ {
@ -145,6 +153,7 @@ const routes: Array<RouteRecordRaw> = [
props: { action: 'reset' }, props: { action: 'reset' },
meta: { meta: {
title: 'user.PASSWORD_RESET', title: 'user.PASSWORD_RESET',
withoutAuth: true,
}, },
}, },
{ {
@ -156,6 +165,7 @@ const routes: Array<RouteRecordRaw> = [
), ),
meta: { meta: {
title: 'user.EMAIL_UPDATE', title: 'user.EMAIL_UPDATE',
withoutChecks: true,
}, },
}, },
{ {
@ -447,6 +457,7 @@ const routes: Array<RouteRecordRaw> = [
component: AboutView, component: AboutView,
meta: { meta: {
title: 'common.ABOUT', title: 'common.ABOUT',
withoutChecks: true,
}, },
}, },
{ {
@ -455,6 +466,7 @@ const routes: Array<RouteRecordRaw> = [
component: PrivacyPolicyView, component: PrivacyPolicyView,
meta: { meta: {
title: 'privacy_policy.TITLE', title: 'privacy_policy.TITLE',
withoutChecks: true,
}, },
}, },
{ {
@ -472,20 +484,6 @@ const router = createRouter({
routes, routes,
}) })
const pathsWithoutAuthentication = [
'/login',
'/password-reset',
'/password-reset/password-updated',
'/password-reset/request',
'/password-reset/sent',
'/register',
'/account-confirmation',
'/account-confirmation/resend',
'/account-confirmation/email-sent',
]
const pathsWithoutChecks = ['/email-update', '/about', '/privacy-policy']
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
if ('title' in to.meta) { if ('title' in to.meta) {
const title = typeof to.meta.title === 'string' ? to.meta.title : '' const title = typeof to.meta.title === 'string' ? to.meta.title : ''
@ -501,18 +499,18 @@ router.beforeEach((to, from, next) => {
store store
.dispatch(AUTH_USER_STORE.ACTIONS.CHECK_AUTH_USER) .dispatch(AUTH_USER_STORE.ACTIONS.CHECK_AUTH_USER)
.then(() => { .then(() => {
if (pathsWithoutChecks.includes(to.path)) { if (to.meta.withoutChecks) {
return next() return next()
} }
if ( if (
store.getters[AUTH_USER_STORE.GETTERS.IS_AUTHENTICATED] && store.getters[AUTH_USER_STORE.GETTERS.IS_AUTHENTICATED] &&
pathsWithoutAuthentication.includes(to.path) to.meta.withoutAuth
) { ) {
return next('/') return next('/')
} }
if ( if (
!store.getters[AUTH_USER_STORE.GETTERS.IS_AUTHENTICATED] && !store.getters[AUTH_USER_STORE.GETTERS.IS_AUTHENTICATED] &&
!pathsWithoutAuthentication.includes(to.path) !to.meta.withoutAuth
) { ) {
const path = const path =
to.path === '/' to.path === '/'