From c4a83f3654b249bb2868193d89304cb56c7c7b16 Mon Sep 17 00:00:00 2001 From: dlohvinov Date: Thu, 21 Mar 2024 22:27:34 +0200 Subject: [PATCH] fix: and another one token loop fix [WTEL-4374] --- .env => .env.production | 0 src/api/auth/auth.js | 10 +++++++--- src/store/modules/auth/auth.js | 18 +++++++++--------- 3 files changed, 16 insertions(+), 12 deletions(-) rename .env => .env.production (100%) diff --git a/.env b/.env.production similarity index 100% rename from .env rename to .env.production diff --git a/src/api/auth/auth.js b/src/api/auth/auth.js index 4a19cc7a..07f92d12 100644 --- a/src/api/auth/auth.js +++ b/src/api/auth/auth.js @@ -47,7 +47,7 @@ const checkSessionByCookies = async () => { if (!accessToken) { try { const response = await instance.get(url, { withCredentials: true }); - localStorage.setItem('access-token', response.access_token); + localStorage.setItem('access-token', response.accessToken); instance.defaults.headers['X-Webitel-Access'] = localStorage.getItem('access-token') || ''; } catch (err) { @@ -60,14 +60,18 @@ const checkCurrentSession = async () => { try { config.silent = true; - if (localStorage.getItem('access-token') === 'undefined') { + const token = localStorage.getItem('access-token'); + if (!token || token !== 'undefined') { clearToken(); + throw new Error('No valid access-token in localStorage'); } await checkSessionByCookies(); const accessToken = await checkSessionByToken(); return accessToken; - } catch {} finally { + } catch (err) { + throw err; + } finally { config.silent = false; } }; diff --git a/src/store/modules/auth/auth.js b/src/store/modules/auth/auth.js index 490a8ae6..ea5417c7 100644 --- a/src/store/modules/auth/auth.js +++ b/src/store/modules/auth/auth.js @@ -3,16 +3,16 @@ import router from '../../../router/router'; import AuthAPI from '../../../api/auth/auth'; const defaultState = () => ({ - username: localStorage.getItem('username') || '', - password: localStorage.getItem('password') || '', + username: localStorage.getItem('auth/username') || '', + password: localStorage.getItem('auth/password') || '', certificate: '', confirmPassword: '', - domain: localStorage.getItem('domain') || '', + domain: localStorage.getItem('auth/domain') || '', }); const state = { ...defaultState(), - rememberCredentials: localStorage.getItem('rememberCredentials') === 'true', + rememberCredentials: localStorage.getItem('auth/rememberCredentials') === 'true', loginProviders: {}, }; @@ -76,7 +76,7 @@ const actions = { ON_AUTH_SUCCESS: async (context, { accessToken }) => { await context.dispatch('CACHE_USER_DATA'); - const redirect = decodeURIComponent(router.currentRoute.query?.redirectTo) || import.meta.env.VITE_START_PAGE_URL; + const redirect = decodeURIComponent(router.currentRoute.value.query?.redirectTo) || import.meta.env.VITE_START_PAGE_URL; if (redirect === 'undefined' || accessToken === 'undefined') { throw new Error(`No redirect (${redirect}) or access token (${accessToken}) provided`); @@ -87,11 +87,11 @@ const actions = { }, CACHE_USER_DATA: (context) => { - if (context.state.domain) localStorage.setItem('domain', context.state.domain); + if (context.state.domain) localStorage.setItem('auth/domain', context.state.domain); if (context.state.rememberCredentials) { - if (context.state.username) localStorage.setItem('username', context.state.username); - if (context.state.password) localStorage.setItem('password', context.state.password); - localStorage.setItem('rememberCredentials', 'true'); + if (context.state.username) localStorage.setItem('auth/username', context.state.username); + if (context.state.password) localStorage.setItem('auth/password', context.state.password); + localStorage.setItem('auth/rememberCredentials', 'true'); } },