fix(twoFactor): unify code login and twoFactor
gitea/salix-front/pipeline/pr-dev This commit looks good
Details
gitea/salix-front/pipeline/pr-dev This commit looks good
Details
This commit is contained in:
parent
bc62dc9a89
commit
ea8aca29a6
|
@ -3,12 +3,14 @@ import { useRole } from './useRole';
|
||||||
import { useAcl } from './useAcl';
|
import { useAcl } from './useAcl';
|
||||||
import { useUserConfig } from './useUserConfig';
|
import { useUserConfig } from './useUserConfig';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import { useRouter } from 'vue-router';
|
||||||
import useNotify from './useNotify';
|
import useNotify from './useNotify';
|
||||||
import { useTokenConfig } from './useTokenConfig';
|
import { useTokenConfig } from './useTokenConfig';
|
||||||
const TOKEN_MULTIMEDIA = 'tokenMultimedia';
|
const TOKEN_MULTIMEDIA = 'tokenMultimedia';
|
||||||
const TOKEN = 'token';
|
const TOKEN = 'token';
|
||||||
|
|
||||||
export function useSession() {
|
export function useSession() {
|
||||||
|
const router = useRouter();
|
||||||
const { notify } = useNotify();
|
const { notify } = useNotify();
|
||||||
let isCheckingToken = false;
|
let isCheckingToken = false;
|
||||||
let intervalId = null;
|
let intervalId = null;
|
||||||
|
@ -102,6 +104,31 @@ export function useSession() {
|
||||||
startInterval();
|
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() {
|
function isLoggedIn() {
|
||||||
const localToken = localStorage.getItem(TOKEN);
|
const localToken = localStorage.getItem(TOKEN);
|
||||||
const sessionToken = sessionStorage.getItem(TOKEN);
|
const sessionToken = sessionStorage.getItem(TOKEN);
|
||||||
|
@ -163,6 +190,7 @@ export function useSession() {
|
||||||
setToken,
|
setToken,
|
||||||
destroy,
|
destroy,
|
||||||
login,
|
login,
|
||||||
|
setLogin,
|
||||||
isLoggedIn,
|
isLoggedIn,
|
||||||
checkValidity,
|
checkValidity,
|
||||||
setSession,
|
setSession,
|
||||||
|
|
|
@ -28,35 +28,10 @@ async function onSubmit() {
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
const { data } = await axios.post('Accounts/login', params);
|
const { data } = await axios.post('Accounts/login', params);
|
||||||
|
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
const {
|
|
||||||
data: { multimediaToken },
|
|
||||||
} = await axios.get('VnUsers/ShareToken', {
|
|
||||||
headers: { Authorization: data.token },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!multimediaToken) return;
|
data.keepLogin = keepLogin.value;
|
||||||
|
await session.setLogin(data);
|
||||||
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' });
|
|
||||||
}
|
|
||||||
} catch (res) {
|
} catch (res) {
|
||||||
if (res.response?.data?.error?.code === 'REQUIRES_2FA') {
|
if (res.response?.data?.error?.code === 'REQUIRES_2FA') {
|
||||||
Notify.create({
|
Notify.create({
|
||||||
|
|
|
@ -25,21 +25,15 @@ async function onSubmit() {
|
||||||
try {
|
try {
|
||||||
params.code = code.value;
|
params.code = code.value;
|
||||||
const { data } = await axios.post('VnUsers/validate-auth', params);
|
const { data } = await axios.post('VnUsers/validate-auth', params);
|
||||||
|
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
await session.login(data.token, params.keepLogin);
|
data.keepLogin = params.keepLogin;
|
||||||
|
await session.setLogin(data);
|
||||||
|
|
||||||
quasar.notify({
|
quasar.notify({
|
||||||
message: t('login.loginSuccess'),
|
message: t('login.loginSuccess'),
|
||||||
type: 'positive',
|
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) {
|
} catch (e) {
|
||||||
quasar.notify({
|
quasar.notify({
|
||||||
message: e.response?.data?.error.message,
|
message: e.response?.data?.error.message,
|
||||||
|
|
|
@ -25,18 +25,20 @@ describe('Login', () => {
|
||||||
vi.spyOn(axios, 'post').mockResolvedValueOnce({ data: { token: 'token' } });
|
vi.spyOn(axios, 'post').mockResolvedValueOnce({ data: { token: 'token' } });
|
||||||
vi.spyOn(axios, 'get').mockImplementation((url) => {
|
vi.spyOn(axios, 'get').mockImplementation((url) => {
|
||||||
if (url === 'VnUsers/acls') return Promise.resolve({ data: [] });
|
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('');
|
expect(vm.session.getToken()).toEqual('');
|
||||||
|
|
||||||
await vm.onSubmit();
|
await vm.onSubmit();
|
||||||
|
|
||||||
expect(vm.session.getToken()).toEqual('token');
|
expect(vm.session.getToken()).toEqual('token');
|
||||||
expect(vm.quasar.notify).toHaveBeenCalledWith(
|
|
||||||
expect.objectContaining({ type: 'positive' })
|
|
||||||
);
|
|
||||||
await vm.session.destroy();
|
await vm.session.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue