Renew token improvements
This commit is contained in:
parent
9658a63b6b
commit
ad9be84b9c
|
@ -4,7 +4,7 @@ import { useUserStore } from '@/stores/user';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import useNotify from '@/composables/useNotify.js';
|
import useNotify from '@/composables/useNotify.js';
|
||||||
import { useAppStore } from '@/stores/app';
|
import { useAppStore } from '@/stores/app';
|
||||||
|
import { Router } from 'src/router';
|
||||||
const { notify } = useNotify();
|
const { notify } = useNotify();
|
||||||
// Be careful when using SSR for cross-request state pollution
|
// Be careful when using SSR for cross-request state pollution
|
||||||
// due to creating a Singleton instance here;
|
// due to creating a Singleton instance here;
|
||||||
|
@ -22,6 +22,8 @@ const onRequestError = error => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const onResponseError = error => {
|
const onResponseError = error => {
|
||||||
|
const userStore = useUserStore();
|
||||||
|
|
||||||
let message = error.message;
|
let message = error.message;
|
||||||
|
|
||||||
const response = error.response;
|
const response = error.response;
|
||||||
|
@ -33,7 +35,14 @@ const onResponseError = error => {
|
||||||
|
|
||||||
notify(message, 'negative');
|
notify(message, 'negative');
|
||||||
|
|
||||||
|
if (userStore.isLoggedIn && response?.status === 401) {
|
||||||
|
if (!Router) return;
|
||||||
|
|
||||||
|
Router.push({ name: 'login' });
|
||||||
|
userStore.destroy(false);
|
||||||
|
} else if (!userStore.isLoggedIn) {
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default boot(({ app }) => {
|
export default boot(({ app }) => {
|
||||||
|
@ -41,6 +50,7 @@ export default boot(({ app }) => {
|
||||||
const appStore = useAppStore();
|
const appStore = useAppStore();
|
||||||
function addToken(config) {
|
function addToken(config) {
|
||||||
if (userStore.token) {
|
if (userStore.token) {
|
||||||
|
if (!config.headers.Authorization)
|
||||||
config.headers.Authorization = userStore.token;
|
config.headers.Authorization = userStore.token;
|
||||||
config.headers['Accept-Language'] = appStore.siteLang;
|
config.headers['Accept-Language'] = appStore.siteLang;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { useI18n } from 'vue-i18n';
|
||||||
|
|
||||||
import { useUserStore } from 'stores/user';
|
import { useUserStore } from 'stores/user';
|
||||||
import useNotify from 'src/composables/useNotify.js';
|
import useNotify from 'src/composables/useNotify.js';
|
||||||
import { useRouter, useRoute } from 'vue-router';
|
import { useRoute } from 'vue-router';
|
||||||
import { storeToRefs } from 'pinia';
|
import { storeToRefs } from 'pinia';
|
||||||
import { useAppStore } from 'src/stores/app';
|
import { useAppStore } from 'src/stores/app';
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@ const { locale } = useI18n({ useScope: 'global' });
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
const appStore = useAppStore();
|
const appStore = useAppStore();
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
const router = useRouter();
|
|
||||||
const { siteLang, localeOptions } = storeToRefs(appStore);
|
const { siteLang, localeOptions } = storeToRefs(appStore);
|
||||||
|
|
||||||
const email = ref(null);
|
const email = ref(null);
|
||||||
|
@ -45,20 +44,14 @@ onMounted(() => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const onLogin = async () => {
|
|
||||||
await userStore.fetchUser();
|
|
||||||
await router.push({ name: 'home' });
|
|
||||||
};
|
|
||||||
|
|
||||||
const login = async () => {
|
const login = async () => {
|
||||||
await userStore.login(email.value, password.value, remember.value);
|
await userStore.login(email.value, password.value, remember.value);
|
||||||
await onLogin();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const loginAsGuest = async () => {
|
const loginAsGuest = async () => {
|
||||||
userStore.isGuest = true;
|
userStore.isGuest = true;
|
||||||
localStorage.setItem('hederaGuest', true);
|
localStorage.setItem('hederaGuest', true);
|
||||||
await onLogin();
|
await userStore.onLogin();
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
@ -18,14 +18,13 @@ import routes from './routes';
|
||||||
* with the Router instance.
|
* with the Router instance.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export default route(function (/* { store, ssrContext } */) {
|
const createHistory = process.env.SERVER
|
||||||
const createHistory = process.env.SERVER
|
|
||||||
? createMemoryHistory
|
? createMemoryHistory
|
||||||
: process.env.VUE_ROUTER_MODE === 'history'
|
: process.env.VUE_ROUTER_MODE === 'history'
|
||||||
? createWebHistory
|
? createWebHistory
|
||||||
: createWebHashHistory;
|
: createWebHashHistory;
|
||||||
|
|
||||||
const Router = createRouter({
|
const Router = createRouter({
|
||||||
scrollBehavior: () => ({ left: 0, top: 0 }),
|
scrollBehavior: () => ({ left: 0, top: 0 }),
|
||||||
routes,
|
routes,
|
||||||
|
|
||||||
|
@ -35,8 +34,11 @@ export default route(function (/* { store, ssrContext } */) {
|
||||||
history: createHistory(
|
history: createHistory(
|
||||||
process.env.MODE === 'ssr' ? void 0 : process.env.VUE_ROUTER_BASE
|
process.env.MODE === 'ssr' ? void 0 : process.env.VUE_ROUTER_BASE
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export { Router };
|
||||||
|
|
||||||
|
export default route(function (/* { store, ssrContext } */) {
|
||||||
Router.beforeEach((to, from, next) => {
|
Router.beforeEach((to, from, next) => {
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
const allowedRoutes = ['login', 'recoverPassword'];
|
const allowedRoutes = ['login', 'recoverPassword'];
|
||||||
|
|
|
@ -25,7 +25,8 @@ export const useUserStore = defineStore('user', () => {
|
||||||
const storage = computed(() =>
|
const storage = computed(() =>
|
||||||
keepLogin.value ? localStorage : sessionStorage
|
keepLogin.value ? localStorage : sessionStorage
|
||||||
);
|
);
|
||||||
const isLoggedIn = computed(() => !!storage.value.getItem(TOKEN));
|
|
||||||
|
const isLoggedIn = computed(() => !!token.value);
|
||||||
|
|
||||||
const init = async _router => {
|
const init = async _router => {
|
||||||
router = _router;
|
router = _router;
|
||||||
|
@ -87,9 +88,9 @@ export const useUserStore = defineStore('user', () => {
|
||||||
let destroyTokenPromises = [];
|
let destroyTokenPromises = [];
|
||||||
try {
|
try {
|
||||||
if (destroyTokens) {
|
if (destroyTokens) {
|
||||||
const { data: isValidToken } = await api.get(
|
const response = await api.get('VnUsers/validateToken');
|
||||||
'VnUsers/validateToken'
|
const isValidToken = response?.data;
|
||||||
);
|
|
||||||
if (isValidToken) {
|
if (isValidToken) {
|
||||||
destroyTokenPromises = Object.entries(tokens).map(
|
destroyTokenPromises = Object.entries(tokens).map(
|
||||||
([key, url]) => destroyToken(url, storage.value, key)
|
([key, url]) => destroyToken(url, storage.value, key)
|
||||||
|
@ -101,6 +102,7 @@ export const useUserStore = defineStore('user', () => {
|
||||||
sessionStorage.clear();
|
sessionStorage.clear();
|
||||||
await Promise.allSettled(destroyTokenPromises);
|
await Promise.allSettled(destroyTokenPromises);
|
||||||
user.value = null;
|
user.value = null;
|
||||||
|
$reset();
|
||||||
stopRenewer();
|
stopRenewer();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -117,15 +119,24 @@ export const useUserStore = defineStore('user', () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchMultimediaToken = async data => {
|
const fetchMultimediaToken = async data => {
|
||||||
const {
|
try {
|
||||||
data: { multimediaToken }
|
const response = await api.get('VnUsers/ShareToken', {
|
||||||
} = await api.get('VnUsers/ShareToken', {
|
|
||||||
headers: { Authorization: data.token }
|
headers: { Authorization: data.token }
|
||||||
});
|
});
|
||||||
|
const multimediaToken = response?.data?.multimediaToken;
|
||||||
return multimediaToken;
|
return multimediaToken;
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error('Error fetching multimedia token');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const onLogin = async () => {
|
||||||
|
await fetchUser();
|
||||||
|
router.push({ name: 'home' });
|
||||||
};
|
};
|
||||||
|
|
||||||
const login = async (username, password, remember) => {
|
const login = async (username, password, remember) => {
|
||||||
|
try {
|
||||||
const params = { user: username, password };
|
const params = { user: username, password };
|
||||||
const { data } = await api.post('Accounts/login', params);
|
const { data } = await api.post('Accounts/login', params);
|
||||||
|
|
||||||
|
@ -141,6 +152,10 @@ export const useUserStore = defineStore('user', () => {
|
||||||
});
|
});
|
||||||
await fetchTokenConfig();
|
await fetchTokenConfig();
|
||||||
startInterval();
|
startInterval();
|
||||||
|
await onLogin();
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error('Error logging in');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const tryAutoLogin = async () => {
|
const tryAutoLogin = async () => {
|
||||||
|
@ -161,7 +176,6 @@ export const useUserStore = defineStore('user', () => {
|
||||||
await api.post('Accounts/logout');
|
await api.post('Accounts/logout');
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
destroy();
|
destroy();
|
||||||
$reset();
|
|
||||||
useAppStore().onLogout();
|
useAppStore().onLogout();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -192,8 +206,8 @@ export const useUserStore = defineStore('user', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
setToken({
|
setToken({
|
||||||
_token: tokenData.data.id,
|
_token: tokenData?.data?.id || '',
|
||||||
_tokenMultimedia: tokenMultimedia.data.id
|
_tokenMultimedia: tokenMultimedia?.data?.id || ''
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -272,6 +286,7 @@ export const useUserStore = defineStore('user', () => {
|
||||||
|
|
||||||
const $reset = () => {
|
const $reset = () => {
|
||||||
token.value = '';
|
token.value = '';
|
||||||
|
tokenMultimedia.value = '';
|
||||||
isGuest.value = false;
|
isGuest.value = false;
|
||||||
user.value = null;
|
user.value = null;
|
||||||
supplantedUser.value = null;
|
supplantedUser.value = null;
|
||||||
|
|
Loading…
Reference in New Issue