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 { useAppStore } from 'stores/app';
import { useUserStore } from 'stores/user';
import {
createRouter,
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) => {
if (from.name === to.name) return;
const app = useAppStore();

View File

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