From 42255acf71ff2d4fd93530ae78acc38caff5aae0 Mon Sep 17 00:00:00 2001 From: dlohvinov Date: Fri, 22 Mar 2024 03:47:36 +0200 Subject: [PATCH] fix: upd to prev [WTEL-4374] --- src/app/router/router.js | 18 ++++++++---------- src/main.js | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/app/router/router.js b/src/app/router/router.js index a874e313f..6a293c5f7 100755 --- a/src/app/router/router.js +++ b/src/app/router/router.js @@ -735,20 +735,18 @@ const router = createRouter({ }); router.beforeEach((to, from, next) => { - debugger; - if (!localStorage.getItem('access-token')) { - if (to.query.accessToken) { - localStorage.setItem('access-token', to.query.accessToken); - const newQuery = { ...to.query }; - delete newQuery.accessToken; - return next({ query: newQuery }); - } + if (!localStorage.getItem('access-token') && !to.query.accessToken) { const desiredUrl = `${window.location.origin}${encodeURIComponent(to.fullPath)}`; const authUrl = import.meta.env.VITE_AUTH_URL; - // console.info(`${authUrl}?redirectTo=${desiredUrl}`); window.location.href = `${authUrl}?redirectTo=${desiredUrl}`; + } else if (to.query.accessToken) { + // assume that access token was set from query before app initialization in main.js + const newQuery = { ...to.query }; + delete newQuery.accessToken; + next({ ...to, query: newQuery }); + } else { + next(); } - return next(); }); export default router; diff --git a/src/main.js b/src/main.js index ce41da575..1caba7c7b 100755 --- a/src/main.js +++ b/src/main.js @@ -24,6 +24,24 @@ const fetchConfig = async () => { return response.json(); }; +const setTokenFromUrl = () => { + try { + const queryMap = window.location.search.slice(1) + .split('&') + .reduce((obj, query) => { + const [key, value] = query.split('='); + obj[key] = value; + return obj; + }, {}); + + if (queryMap.accessToken) { + localStorage.setItem('access-token', queryMap.accessToken); + } + } catch (err) { + console.error('Error restoring token from url', err); + } +}; + const initSession = async () => store.dispatch('userinfo/OPEN_SESSION', { instance }); const createVueInstance = () => { @@ -45,6 +63,8 @@ const createVueInstance = () => { (async () => { let config = {}; try { + setTokenFromUrl(); + config = await fetchConfig(); await initSession(); } catch (err) {