Token changes
gitea/hedera-web/pipeline/pr-4922-vueMigration This commit looks good Details

This commit is contained in:
William Buezas 2024-10-22 10:41:09 -03:00
parent cfff9f7aeb
commit cb1542937a
2 changed files with 14 additions and 4 deletions

View File

@ -1,5 +1,6 @@
import { route } from 'quasar/wrappers'; import { route } from 'quasar/wrappers';
import { useAppStore } from 'stores/app'; import { useAppStore } from 'stores/app';
import { useUserStore } from 'stores/user';
import { import {
createRouter, createRouter,
createMemoryHistory, createMemoryHistory,
@ -36,6 +37,16 @@ export default route(function (/* { store, ssrContext } */) {
) )
}); });
Router.beforeEach((to, from, next) => {
const userStore = useUserStore();
if (!userStore.storage.getItem('token') && to.name !== 'login') {
return next({ name: 'login' });
}
next();
});
Router.afterEach((to, from) => { Router.afterEach((to, from) => {
if (from.name === to.name) return; if (from.name === to.name) return;
const app = useAppStore(); const app = useAppStore();

View File

@ -22,16 +22,16 @@ export const useUserStore = defineStore('user', () => {
const tokenConfig = ref(null); const tokenConfig = ref(null);
let router; let router;
const loggedIn = computed(() => !!token.value);
const storage = computed(() => const storage = computed(() =>
keepLogin.value ? localStorage : sessionStorage keepLogin.value ? localStorage : sessionStorage
); );
const isLoggedIn = computed(() => !!storage.value.getItem(TOKEN));
const init = async _router => { const init = async _router => {
router = _router; router = _router;
isGuest.value = localStorage.getItem('hederaGuest') || false; isGuest.value = localStorage.getItem('hederaGuest') || false;
await getToken(); await getToken();
if (!loggedIn.value) { if (!isLoggedIn.value) {
const autoLoginStatus = await tryAutoLogin(); const autoLoginStatus = await tryAutoLogin();
if (!autoLoginStatus) { if (!autoLoginStatus) {
router.push({ name: 'login' }); router.push({ name: 'login' });
@ -285,7 +285,6 @@ export const useUserStore = defineStore('user', () => {
watch( watch(
[mainUser, supplantedUser], [mainUser, supplantedUser],
() => (user.value = supplantedUser.value || mainUser.value), () => (user.value = supplantedUser.value || mainUser.value),
{ immediate: true } { immediate: true }
); );
@ -300,7 +299,7 @@ export const useUserStore = defineStore('user', () => {
intervalId, intervalId,
isCheckingToken, isCheckingToken,
tokenConfig, tokenConfig,
loggedIn, isLoggedIn,
storage, storage,
getToken, getToken,
getTokenMultimedia, getTokenMultimedia,