Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/double call login api[WTEL-4584] #84

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/api/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const checkCurrentSession = async () => {
const token = localStorage.getItem('access-token');
if (!token || token === 'undefined') {
clearToken();
throw new Error('No valid access-token in localStorage');
console.info('No valid access-token in localStorage present at checkCurrentSession');
}

await checkSessionByCookies();
Expand All @@ -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
Loading