forked from verdnatura/salix-front
Merge pull request '#7185 - Multiple toast errors when logout' (!521) from 7185_logoutErrorsToast into dev
Reviewed-on: verdnatura/salix-front#521 Reviewed-by: Alex Moreno <alexm@verdnatura.es>
This commit is contained in:
commit
67e6386939
|
@ -55,10 +55,10 @@ const onResponseError = (error) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session.isLoggedIn() && response?.status === 401) {
|
if (session.isLoggedIn() && response?.status === 401) {
|
||||||
session.destroy();
|
session.destroy(false);
|
||||||
const hash = window.location.hash;
|
const hash = window.location.hash;
|
||||||
const url = hash.slice(1);
|
const url = hash.slice(1);
|
||||||
Router.push({ path: url });
|
Router.push(`/login?redirect=${url}`);
|
||||||
} else if (!session.isLoggedIn()) {
|
} else if (!session.isLoggedIn()) {
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,31 +58,37 @@ export function useSession() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async function destroy() {
|
async function destroy(destroyTokens = true) {
|
||||||
const tokens = {
|
const tokens = {
|
||||||
tokenMultimedia: 'Accounts/logout',
|
tokenMultimedia: 'Accounts/logout',
|
||||||
token: 'VnUsers/logout',
|
token: 'VnUsers/logout',
|
||||||
};
|
};
|
||||||
const storage = keepLogin() ? localStorage : sessionStorage;
|
const storage = keepLogin() ? localStorage : sessionStorage;
|
||||||
|
let destroyTokenPromises = [];
|
||||||
|
try {
|
||||||
|
if (destroyTokens) {
|
||||||
|
const { data: isValidToken } = await axios.get('VnUsers/validateToken');
|
||||||
|
if (isValidToken)
|
||||||
|
destroyTokenPromises = Object.entries(tokens).map(([key, url]) =>
|
||||||
|
destroyToken(url, storage, key)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
localStorage.clear();
|
||||||
|
sessionStorage.clear();
|
||||||
|
await Promise.allSettled(destroyTokenPromises);
|
||||||
|
const { setUser } = useState();
|
||||||
|
|
||||||
for (const [key, url] of Object.entries(tokens)) {
|
setUser({
|
||||||
await destroyToken(url, storage, key);
|
id: 0,
|
||||||
|
name: '',
|
||||||
|
nickname: '',
|
||||||
|
lang: '',
|
||||||
|
darkMode: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
stopRenewer();
|
||||||
}
|
}
|
||||||
|
|
||||||
localStorage.clear();
|
|
||||||
sessionStorage.clear();
|
|
||||||
|
|
||||||
const { setUser } = useState();
|
|
||||||
|
|
||||||
setUser({
|
|
||||||
id: 0,
|
|
||||||
name: '',
|
|
||||||
nickname: '',
|
|
||||||
lang: '',
|
|
||||||
darkMode: null,
|
|
||||||
});
|
|
||||||
|
|
||||||
stopRenewer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function login(data) {
|
async function login(data) {
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
/// <reference types="cypress" />
|
||||||
|
describe('Logout', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
cy.login('developer');
|
||||||
|
cy.visit(`/#/dashboard`);
|
||||||
|
cy.waitForElement('.q-page', 6000);
|
||||||
|
});
|
||||||
|
describe('by user', () => {
|
||||||
|
it('should logout', () => {
|
||||||
|
cy.get(
|
||||||
|
'#user > .q-btn__content > .q-avatar > .q-avatar__content > .q-img > .q-img__container > .q-img__image'
|
||||||
|
).click();
|
||||||
|
cy.get('.block').click();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('not user', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
cy.intercept('GET', '**/VnUsers/acl', {
|
||||||
|
statusCode: 401,
|
||||||
|
body: {
|
||||||
|
error: {
|
||||||
|
statusCode: 401,
|
||||||
|
name: 'Error',
|
||||||
|
message: 'Authorization Required',
|
||||||
|
code: 'AUTHORIZATION_REQUIRED',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
statusMessage: 'AUTHORIZATION_REQUIRED',
|
||||||
|
}).as('someRoute');
|
||||||
|
});
|
||||||
|
it('when token not exists', () => {
|
||||||
|
cy.reload();
|
||||||
|
cy.get('.q-notification__message').should(
|
||||||
|
'have.text',
|
||||||
|
'Authorization Required'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -55,6 +55,7 @@ describe('session', () => {
|
||||||
expect(user.value).toEqual(previousUser);
|
expect(user.value).toEqual(previousUser);
|
||||||
|
|
||||||
vi.spyOn(axios, 'post').mockResolvedValue({ data: true });
|
vi.spyOn(axios, 'post').mockResolvedValue({ data: true });
|
||||||
|
vi.spyOn(axios, 'get').mockResolvedValue({ data: true });
|
||||||
await session.destroy();
|
await session.destroy();
|
||||||
|
|
||||||
user = state.getUser();
|
user = state.getUser();
|
||||||
|
|
Loading…
Reference in New Issue