add: guard in router

This commit is contained in:
Alexandro Uc Santos
2024-12-14 17:46:38 -06:00
parent e24f96c061
commit fa56d25258
9 changed files with 148 additions and 37 deletions

View File

@@ -3,9 +3,7 @@ import { ref, onMounted } from "vue";
import { useRoute, useRouter } from 'vue-router';
import { renewToken } from '../services/auth';
import {useNotificationsStore} from './notifications';
import {useCompanyStore} from './company';
import { useLoadsStore } from "./loads";
import { useVehiclesStore } from "./vehicles";
import { updateMyUserProfile } from "../services/company";
export const useAuthStore = defineStore('auth', () => {
@@ -13,16 +11,14 @@ export const useAuthStore = defineStore('auth', () => {
const router = useRouter();
const route = useRoute();
const noty = useNotificationsStore();
const company = useCompanyStore();
const loadStore = useLoadsStore();
const vehiclesStore = useVehiclesStore();
const notyStore = useNotificationsStore();
const sesion = ref('')
const checking = ref(false);
const authStatus = ref('checking');
const token = ref('')
const lang = ref('es');
const user = ref(null);
const isAuthenticated = ref(false);
const publicNames = [
'terms-conditions',
'notice-privacy',
@@ -34,15 +30,10 @@ export const useAuthStore = defineStore('auth', () => {
'register-company',
]
onMounted( async() => {
checkSession();
});
const checkSession = async() => {
const session = localStorage.getItem('session');
authStatus.value = 'checking';
if(session && !publicNames.includes(route.name)) {
if((session && !publicNames.includes(route.name)) && isAuthenticated.value === false && checking.value === false) {
checking.value = true;
const resp = await renewToken();
if(resp.msg === 'success') {
@@ -53,22 +44,24 @@ export const useAuthStore = defineStore('auth', () => {
localStorage.setItem('access', resp.data.accessToken);
localStorage.setItem('id', resp.data.user.company._id);
checking.value = false;
isAuthenticated.value = true;
} else {
noty.show = true;
noty.text = 'Sesión ha expirado, ingresa nuevamente';
noty.error = true;
checking.value = false;
isAuthenticated.value = false;
router.push({name: 'login'});
}
}
}
authStatus.value = 'completed';
}
const authenticationPromise = new Promise((resolve) => {
onMounted(() => {
resolve('success');
});
});
// const authenticationPromise = new Promise((resolve) => {
// onMounted(() => {
// resolve('success');
// });
// });
const updateProfile = async(data) => {
const response = await updateMyUserProfile(data);
@@ -90,17 +83,7 @@ export const useAuthStore = defineStore('auth', () => {
user.value = null;
checking.value = false;
authStatus.value = 'checking'
notyStore.notifications = [];
notyStore.newNoty = false;
notyStore.show = false;
notyStore.openNotifications = false;
notyStore.toggleProfile = false;
company.clear();
loadStore.clear();
vehiclesStore.clear();
router.push({name: 'login'});
}
return {
@@ -111,8 +94,9 @@ export const useAuthStore = defineStore('auth', () => {
checking,
authStatus,
checkSession,
authenticationPromise,
// authenticationPromise,
updateProfile,
lang
lang,
isAuthenticated
}
});

View File

@@ -34,6 +34,15 @@ export const useNotificationsStore = defineStore('notifications', () => {
return notifications.value = notifications.value.filter(noty => noty._id !== id);
}
const clear = () => {
notifications.value = [];
newNoty.value = false;
show.value = false;
openNotifications.value = false;
toggleProfile.value = false;
}
return {
text,
@@ -45,6 +54,7 @@ export const useNotificationsStore = defineStore('notifications', () => {
removeNoty,
toggleProfile,
openNotifications,
toggleNotifications
toggleNotifications,
clear
}
});