Router not longer redirects to dashboard on page reload
gitea/salix-front/pipeline/head There was a failure building this commit Details

This commit is contained in:
Joan Sanchez 2022-04-21 14:19:15 +02:00
parent 25244272fd
commit d90fe72452
3 changed files with 32 additions and 26 deletions

View File

@ -1,18 +1,15 @@
<script setup> <script setup>
import { onMounted, computed } from 'vue'; import { onMounted, computed } from 'vue';
import { Dark, useQuasar } from 'quasar'; import { Dark } from 'quasar';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
import axios from 'axios'; import axios from 'axios';
import { useState } from 'src/composables/useState'; import { useState } from 'src/composables/useState';
import { useSession } from 'src/composables/useSession'; import { useSession } from 'src/composables/useSession';
import { useRole } from 'src/composables/useRole';
const quasar = useQuasar();
const state = useState(); const state = useState();
const session = useSession(); const session = useSession();
const role = useRole();
const router = useRouter(); const router = useRouter();
const { t, locale } = useI18n(); const { t, locale } = useI18n();
@ -29,19 +26,7 @@ const user = state.getUser();
const token = session.getToken(); const token = session.getToken();
onMounted(async () => { onMounted(async () => {
try {
const stateRoles = state.getRoles().value;
if (stateRoles.length === 0) {
await role.fetch();
}
updatePreferences(); updatePreferences();
} catch (error) {
quasar.notify({
message: t('errors.statusUnauthorized'),
type: 'negative',
});
logout();
}
}); });
function updatePreferences() { function updatePreferences() {

View File

@ -1,11 +1,16 @@
import { route } from 'quasar/wrappers'; import { route } from 'quasar/wrappers';
import { createRouter, createMemoryHistory, createWebHistory, createWebHashHistory } from 'vue-router'; import { createRouter, createMemoryHistory, createWebHistory, createWebHashHistory } from 'vue-router';
import { Notify } from 'quasar';
import routes from './routes'; import routes from './routes';
import { i18n } from 'src/boot/i18n'; import { i18n } from 'src/boot/i18n';
import { useState } from 'src/composables/useState';
import { useSession } from 'src/composables/useSession'; import { useSession } from 'src/composables/useSession';
import { useRole } from 'src/composables/useRole'; import { useRole } from 'src/composables/useRole';
const state = useState();
const session = useSession(); const session = useSession();
const role = useRole(); const role = useRole();
const { t } = i18n.global;
/* /*
* If not building with SSR mode, you can * If not building with SSR mode, you can
@ -33,12 +38,29 @@ export default route(function (/* { store, ssrContext } */) {
history: createHistory(process.env.MODE === 'ssr' ? void 0 : process.env.VUE_ROUTER_BASE), history: createHistory(process.env.MODE === 'ssr' ? void 0 : process.env.VUE_ROUTER_BASE),
}); });
Router.beforeEach((to, from, next) => { Router.beforeEach(async (to, from, next) => {
const { isLoggedIn } = session; const { isLoggedIn } = session;
if (!isLoggedIn() && to.name !== 'Login') { if (!isLoggedIn() && to.name !== 'Login') {
next({ name: 'Login', query: { redirect: to.fullPath } }); return next({ name: 'Login', query: { redirect: to.fullPath } });
} else { }
if (isLoggedIn()) {
try {
const stateRoles = state.getRoles().value;
if (stateRoles.length === 0) {
await role.fetch();
}
} catch (error) {
Notify.create({
message: t('errors.statusUnauthorized'),
type: 'negative',
});
session.destroy();
return next({ path: '/login' });
}
const matches = to.matched; const matches = to.matched;
const hasRequiredRoles = matches.every(route => { const hasRequiredRoles = matches.every(route => {
const meta = route.meta; const meta = route.meta;
@ -47,16 +69,15 @@ export default route(function (/* { store, ssrContext } */) {
return true; return true;
}); });
if (hasRequiredRoles) { if (!hasRequiredRoles) {
return next({ path: '/' });
}
}
next(); next();
} else {
next({ path: '/' });
}
}
}); });
Router.afterEach((to) => { Router.afterEach((to) => {
const { t } = i18n.global;
let title = t(`login.title`); let title = t(`login.title`);
const matches = to.matched; const matches = to.matched;

View File

@ -29,7 +29,7 @@ export default {
path: 'create', path: 'create',
name: 'CustomerCreate', name: 'CustomerCreate',
meta: { meta: {
title: 'create' title: 'createCustomer'
}, },
component: () => import('src/pages/Customer/CustomerCreate.vue'), component: () => import('src/pages/Customer/CustomerCreate.vue'),
}, },