Skip to content

Commit

Permalink
fix: and another one token loop fix [WTEL-4374]
Browse files Browse the repository at this point in the history
  • Loading branch information
dlohvinov committed Mar 22, 2024
1 parent 2b66224 commit 812a48b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
File renamed without changes.
10 changes: 7 additions & 3 deletions src/api/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
};
Expand Down
22 changes: 12 additions & 10 deletions src/store/modules/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
};

Expand Down Expand Up @@ -76,22 +76,24 @@ 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`);
}

const url = `${redirect}?accessToken=${accessToken}`;
const url = redirect.includes('?')
? `${redirect}&accessToken=${accessToken}`
: `${redirect}?accessToken=${accessToken}`;
window.location.href = url;
},

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');
}
},

Expand Down

0 comments on commit 812a48b

Please sign in to comment.