Skip to content

Commit

Permalink
fix: upd to prev [WTEL-4374]
Browse files Browse the repository at this point in the history
  • Loading branch information
dlohvinov committed Mar 22, 2024
1 parent 0022b99 commit 42255ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/app/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
20 changes: 20 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -45,6 +63,8 @@ const createVueInstance = () => {
(async () => {
let config = {};
try {
setTokenFromUrl();

config = await fetchConfig();
await initSession();
} catch (err) {
Expand Down

0 comments on commit 42255ac

Please sign in to comment.