diff --git a/src/composables/useSession.js b/src/composables/useSession.js index 10791c9c8..633a30bb0 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -3,12 +3,14 @@ import { useRole } from './useRole'; import { useAcl } from './useAcl'; import { useUserConfig } from './useUserConfig'; import axios from 'axios'; +import { useRouter } from 'vue-router'; import useNotify from './useNotify'; import { useTokenConfig } from './useTokenConfig'; const TOKEN_MULTIMEDIA = 'tokenMultimedia'; const TOKEN = 'token'; export function useSession() { + const router = useRouter(); const { notify } = useNotify(); let isCheckingToken = false; let intervalId = null; @@ -102,6 +104,31 @@ export function useSession() { startInterval(); } + async function setLogin(data) { + const { + data: { multimediaToken }, + } = await axios.get('VnUsers/ShareToken', { + headers: { Authorization: data.token }, + }); + + if (!multimediaToken) return; + + await login({ + ...data, + created: Date.now(), + tokenMultimedia: multimediaToken.id, + }); + + notify('login.loginSuccess', 'positive'); + + const currentRoute = router.currentRoute.value; + if (currentRoute.query?.redirect) { + router.push(currentRoute.query.redirect); + } else { + router.push({ name: 'Dashboard' }); + } + } + function isLoggedIn() { const localToken = localStorage.getItem(TOKEN); const sessionToken = sessionStorage.getItem(TOKEN); @@ -163,6 +190,7 @@ export function useSession() { setToken, destroy, login, + setLogin, isLoggedIn, checkValidity, setSession, diff --git a/src/pages/Login/LoginMain.vue b/src/pages/Login/LoginMain.vue index 5a3490f50..4eb21f573 100644 --- a/src/pages/Login/LoginMain.vue +++ b/src/pages/Login/LoginMain.vue @@ -28,35 +28,10 @@ async function onSubmit() { }; try { const { data } = await axios.post('Accounts/login', params); - if (!data) return; - const { - data: { multimediaToken }, - } = await axios.get('VnUsers/ShareToken', { - headers: { Authorization: data.token }, - }); - if (!multimediaToken) return; - - const login = { - ...data, - created: Date.now(), - tokenMultimedia: multimediaToken.id, - keepLogin: keepLogin.value, - }; - await session.login(login); - - quasar.notify({ - message: t('login.loginSuccess'), - type: 'positive', - }); - - const currentRoute = router.currentRoute.value; - if (currentRoute.query && currentRoute.query.redirect) { - router.push(currentRoute.query.redirect); - } else { - router.push({ name: 'Dashboard' }); - } + data.keepLogin = keepLogin.value; + await session.setLogin(data); } catch (res) { if (res.response?.data?.error?.code === 'REQUIRES_2FA') { Notify.create({ diff --git a/src/pages/Login/TwoFactor.vue b/src/pages/Login/TwoFactor.vue index 8ba1a1f7d..55fc7a1d9 100644 --- a/src/pages/Login/TwoFactor.vue +++ b/src/pages/Login/TwoFactor.vue @@ -25,21 +25,15 @@ async function onSubmit() { try { params.code = code.value; const { data } = await axios.post('VnUsers/validate-auth', params); - if (!data) return; - await session.login(data.token, params.keepLogin); + data.keepLogin = params.keepLogin; + await session.setLogin(data); + quasar.notify({ message: t('login.loginSuccess'), type: 'positive', }); - - const currentRoute = router.currentRoute.value; - if (currentRoute.query && currentRoute.query.redirect) { - router.push(currentRoute.query.redirect); - } else { - router.push({ name: 'Dashboard' }); - } } catch (e) { quasar.notify({ message: e.response?.data?.error.message, diff --git a/test/vitest/__tests__/pages/Login/Login.spec.js b/test/vitest/__tests__/pages/Login/Login.spec.js index 9b9968736..5ab4cee9e 100644 --- a/test/vitest/__tests__/pages/Login/Login.spec.js +++ b/test/vitest/__tests__/pages/Login/Login.spec.js @@ -25,18 +25,20 @@ describe('Login', () => { vi.spyOn(axios, 'post').mockResolvedValueOnce({ data: { token: 'token' } }); vi.spyOn(axios, 'get').mockImplementation((url) => { if (url === 'VnUsers/acls') return Promise.resolve({ data: [] }); - return Promise.resolve({data: { roles: [], user: expectedUser , multimediaToken: {id:'multimediaToken' }}}); + return Promise.resolve({ + data: { + roles: [], + user: expectedUser, + multimediaToken: { id: 'multimediaToken' }, + }, + }); }); - vi.spyOn(vm.quasar, 'notify'); expect(vm.session.getToken()).toEqual(''); await vm.onSubmit(); expect(vm.session.getToken()).toEqual('token'); - expect(vm.quasar.notify).toHaveBeenCalledWith( - expect.objectContaining({ type: 'positive' }) - ); await vm.session.destroy(); });