Skip to content
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: 7 additions & 5 deletions src/partners/services/partners-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ function removeToken(): void {
}

async function logIn(email: string, password: string, code?: string): Promise<{ twoFactorSetupRequired: boolean }> {
const response = await partnersAxios.post<{ token: string; twoFactorSetupRequired: boolean }>('/partners/login', {
email,
password,
code,
});
const response = await captchaService.withCaptcha('Login', (headers) =>
partnersAxios.post<{ token: string; twoFactorSetupRequired: boolean }>(
'/partners/login',
{ email, password, code },
{ headers },
),
);
setToken(response.data.token);
return { twoFactorSetupRequired: response.data.twoFactorSetupRequired };
}
Expand Down
22 changes: 16 additions & 6 deletions src/services/login-captcha.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { describe, expect, test } from 'vitest';
import managementAxios from '../management/core/management-axios';
import partnersAxios from '../partners/core/partners-axios';
import wholesalersAxios from '../wholesalers/core/wholesalers-axios';
import { managementAuthService } from '../management/services/management-auth.service';
import { partnersAuthService } from '../partners/services/partners-auth.service';
import { wholesalersAuthService } from '../wholesalers/services/wholesalers-auth.service';
import { mockAdapter } from '../testing/mock-axios-adapter';
import { stubLocation } from '../testing/stub-location';
import { CaptchaUnavailableError, captchaService } from './captcha.service';

describe('management login', () => {
stubLocation('/management/login');
describe('console logins', () => {
stubLocation('/login');

test('when the backend rejects the captcha, then logIn fails with a CaptchaUnavailableError', async () => {
managementAxios.defaults.adapter = mockAdapter(() => ({ status: 403, data: { code: 'CAPTCHA_FAILED' } }));
describe.each([
['management', managementAxios, managementAuthService],
['partners', partnersAxios, partnersAuthService],
['wholesalers', wholesalersAxios, wholesalersAuthService],
])('when the %s backend rejects the captcha', (_, axiosInstance, authService) => {
test('then logIn fails with a CaptchaUnavailableError', async () => {
axiosInstance.defaults.adapter = mockAdapter(() => ({ status: 403, data: { code: 'CAPTCHA_FAILED' } }));

const error = await managementAuthService.logIn('[email protected]', 'secret').catch((e: unknown) => e);
const error = await authService.logIn('[email protected]', 'secret').catch((e: unknown) => e);

expect(error).toBeInstanceOf(CaptchaUnavailableError);
expect(error).toBeInstanceOf(CaptchaUnavailableError);
});
});
});

Expand Down
10 changes: 7 additions & 3 deletions src/wholesalers/services/wholesalers-auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import wholesalersAxios from '../core/wholesalers-axios';
import { captchaService } from '../../services/captcha.service';

const WHOLESALERS_TOKEN_KEY = 'wholesalersToken';

Expand All @@ -15,9 +16,12 @@ function removeToken(): void {
}

async function logIn(email: string, password: string, code?: string): Promise<{ twoFactorSetupRequired: boolean }> {
const response = await wholesalersAxios.post<{ token: string; twoFactorSetupRequired?: boolean }>(
'/wholesalers/login',
{ email, password, code },
const response = await captchaService.withCaptcha('Login', (headers) =>
wholesalersAxios.post<{ token: string; twoFactorSetupRequired?: boolean }>(
'/wholesalers/login',
{ email, password, code },
{ headers },
),
);
setToken(response.data.token);
return { twoFactorSetupRequired: !!response.data.twoFactorSetupRequired };
Expand Down
Loading