Skip to content

Commit

Permalink
Merge pull request #84 from webitel/hotfix/double-call-login-api
Browse files Browse the repository at this point in the history
Hotfix/double call login api
  • Loading branch information
Lera24 authored May 23, 2024
2 parents 9b2e4ef + 76cf7ec commit 724b550
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 54 deletions.
10 changes: 1 addition & 9 deletions src/api/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,6 @@ const checkCurrentSession = async () => {
}
};

const loadServiceProviders = async ({ domain }) => {
const baseUrl = '/login';
const url = `${baseUrl}?domain=${domain}`;
const response = await instance.get(url);
return response;
};

const clearToken = () => {
localStorage.removeItem('access-token');
instance.defaults.headers['X-Webitel-Access'] = '';
Expand All @@ -116,7 +109,7 @@ const checkDomainExistence = async (domain) => {
const baseUrl = '/login';
const url = `${baseUrl}?domain=${domain}`;
try {
await instance.get(url);
return await instance.get(url);
} catch (err) {
throw err;
}
Expand All @@ -127,7 +120,6 @@ const AuthAPI = {
login2fa,
register,
checkCurrentSession,
loadServiceProviders,
checkDomainExistence,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ describe('TheLoginProviders', () => {
expect(wrapper.exists()).toBe(true);
});

it('loads service providers initially', () => {
const mock = vi.spyOn(store, 'dispatch').mockImplementationOnce(vi.fn());
const wrapper = shallowMount(TheLoginProviders, {
global: {
plugins: [store],
},
});
expect(mock).toHaveBeenCalledWith('auth/LOAD_SERVICE_PROVIDERS');
});

it('should render service providers', () => {
const loginProviders = {
[ServiceProvider.ADFS]: {},
Expand Down
6 changes: 0 additions & 6 deletions src/components/auth/login/providers/the-login-providers.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ const serviceProviders = computed(() => {
function openProvider({ ticket }) {
return store.dispatch('auth/EXECUTE_PROVIDER', { ticket });
}
function loadAvailableProviders() {
return store.dispatch('auth/LOAD_SERVICE_PROVIDERS');
}
loadAvailableProviders();
</script>

<style lang="scss" scoped>
Expand Down
28 changes: 9 additions & 19 deletions src/store/modules/auth/__tests__/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,6 @@ describe('auth store', () => {
expect(mock).toHaveBeenCalledWith(state);
});

it('LOAD_SERVICE_PROVIDERS action calls AuthAPI.loadServiceProviders with domain from state', async () => {
const state = {
domain: 'domain',
};

const federation = {};

context.state = state;

const mock = vi.spyOn(authAPI, 'loadServiceProviders').mockImplementationOnce(() => ({ federation }));
await auth.actions.LOAD_SERVICE_PROVIDERS(context);
expect(mock).toHaveBeenCalledWith({ domain: state.domain });
expect(context.commit).toHaveBeenCalledWith('SET_SERVICE_PROVIDERS', federation);
});

it('CHECK_CURRENT_SESSION action calls AuthAPI.checkCurrentSession', async () => {
const accessToken = 'vi';
const mock = vi.spyOn(authAPI, 'checkCurrentSession')
Expand Down Expand Up @@ -111,11 +96,16 @@ describe('auth store', () => {
});

it('CHECK_DOMAIN action calls AuthAPI.checkDomainExistence with domain from state', async () => {
const domain = 'domain';
context.state.domain = domain;
const state = {
domain: 'domain',
federation: {},
};

context.state = state;

const mock = vi.spyOn(authAPI, 'checkDomainExistence').mockImplementationOnce(vi.fn());
const mock = vi.spyOn(authAPI, 'checkDomainExistence').mockImplementationOnce(() => ({ federation: state.federation }));
await auth.actions.CHECK_DOMAIN(context);
expect(mock).toHaveBeenCalledWith(domain);
expect(mock).toHaveBeenCalledWith(state.domain);
expect(context.commit).toHaveBeenCalledWith('SET_SERVICE_PROVIDERS', state.federation);
});
});
15 changes: 5 additions & 10 deletions src/store/modules/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ const actions = {
}
},

LOAD_SERVICE_PROVIDERS: async (context) => {
const domain = context.state.domain;
const response = await AuthAPI.loadServiceProviders({ domain });
const { federation = {}, enabledTfa } = response;
context.commit('SET_SERVICE_PROVIDERS', federation);
await context.dispatch('SET_PROPERTY', { prop: 'enabledTfa', value: enabledTfa });
},

EXECUTE_PROVIDER: (context, { ticket }) => {
const baseUrl = `${import.meta.env.VITE_API_URL}/login`;
const query = {
Expand Down Expand Up @@ -118,8 +110,11 @@ const actions = {
if (context.state.domain) localStorage.setItem('auth/domain', context.state.domain);
},

CHECK_DOMAIN: (context, domain = context.state.domain) => {
return AuthAPI.checkDomainExistence(domain);
CHECK_DOMAIN: async (context, domain = context.state.domain) => {
const response = await AuthAPI.checkDomainExistence(domain);
const { federation = {}, enabledTfa } = response;
context.commit('SET_SERVICE_PROVIDERS', federation);
await context.dispatch('SET_PROPERTY', { prop: 'enabledTfa', value: enabledTfa });
},

SET_PROPERTY: (context, { prop, value }) => {
Expand Down

0 comments on commit 724b550

Please sign in to comment.