Merge pull request 'fix(twoFactor): unify code login and twoFactor' (!603) from 7854-fix_twoFator into dev
gitea/salix-front/pipeline/head This commit looks good
Details
gitea/salix-front/pipeline/head This commit looks good
Details
Reviewed-on: #603 Reviewed-by: Jorge Penades <jorgep@verdnatura.es>
This commit is contained in:
commit
adc3b0307e
|
@ -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,
|
||||
|
|
|
@ -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({
|
||||
|
|
|
@ -25,21 +25,10 @@ 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);
|
||||
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 = params.keepLogin;
|
||||
await session.setLogin(data);
|
||||
} catch (e) {
|
||||
quasar.notify({
|
||||
message: e.response?.data?.error.message,
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue