diff --git a/src/App.tsx b/src/App.tsx index 1245d0d9..029594f0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -40,6 +40,8 @@ import { WholesalersAuthRoute } from './wholesalers/components/WholesalersAuthRo import { WholesalersPartnersPage } from './wholesalers/views/WholesalersPartnersPage'; import { WholesalersPartnerDetailPage } from './wholesalers/views/WholesalersPartnerDetailPage'; import { WholesalersSettingsPage } from './wholesalers/views/WholesalersSettingsPage'; +import { WholesalersForgotPasswordPage } from './wholesalers/views/WholesalersForgotPasswordPage'; +import { WholesalersResetPasswordPage } from './wholesalers/views/WholesalersResetPasswordPage'; const DEFAULT_LOGIN_PATH = '/subaccount/login'; @@ -103,6 +105,8 @@ export function App() { {import.meta.env.VITE_ENABLE_WHOLESALER_PANEL === 'true' && ( <> } /> + } /> + } /> }> } /> } /> diff --git a/src/utils/publicAuthPage.test.ts b/src/utils/publicAuthPage.test.ts new file mode 100644 index 00000000..1cba7194 --- /dev/null +++ b/src/utils/publicAuthPage.test.ts @@ -0,0 +1,18 @@ +import { describe, expect, test } from 'vitest'; +import { isPublicAuthPage } from './publicAuthPage'; + +describe('isPublicAuthPage', () => { + test.each(['/wholesalers/login', '/wholesalers/forgot-password', '/wholesalers/reset-password'])( + 'when the path is %s, then it is a public auth page', + (pathname) => { + expect(isPublicAuthPage(pathname)).toBe(true); + }, + ); + + test.each(['/wholesalers/partners', '/wholesalers/settings', '/wholesalers/reset-password/extra'])( + 'when the path is %s, then it is not a public auth page', + (pathname) => { + expect(isPublicAuthPage(pathname)).toBe(false); + }, + ); +}); diff --git a/src/utils/publicAuthPage.ts b/src/utils/publicAuthPage.ts new file mode 100644 index 00000000..bd9c7b04 --- /dev/null +++ b/src/utils/publicAuthPage.ts @@ -0,0 +1,5 @@ +const PUBLIC_AUTH_PAGE_SUFFIXES = ['/login', '/forgot-password', '/reset-password']; + +export function isPublicAuthPage(pathname: string): boolean { + return PUBLIC_AUTH_PAGE_SUFFIXES.some((suffix) => pathname.endsWith(suffix)); +} diff --git a/src/wholesalers/console-branding.tsx b/src/wholesalers/console-branding.tsx new file mode 100644 index 00000000..8b5f977d --- /dev/null +++ b/src/wholesalers/console-branding.tsx @@ -0,0 +1,12 @@ +export const wholesalersConsoleBranding = { + consoleTitle: 'Wholesalers Console', + rightHeadline: ( + <> + Object Storage +
+ Wholesalers + + ), + rightDescription: 'Create partners, monitor their storage usage, and manage aggregated billing from one place.', + rightFeaturePills: ['Partner management', 'Usage monitoring', 'Aggregated billing'], +}; diff --git a/src/wholesalers/core/wholesalers-axios.test.ts b/src/wholesalers/core/wholesalers-axios.test.ts index 993c8530..ed46655b 100644 --- a/src/wholesalers/core/wholesalers-axios.test.ts +++ b/src/wholesalers/core/wholesalers-axios.test.ts @@ -1,8 +1,37 @@ -import { describe } from 'vitest'; +import { afterEach, describe, expect, test, vi } from 'vitest'; import wholesalersAxios from './wholesalers-axios'; import { wholesalersAuthService } from '../services/wholesalers-auth.service'; +import notificationsService from '../../services/notifications.service'; import { describeScopedAxiosContract } from '../../testing/scoped-axios-contract'; +import { mockAdapter } from '../../testing/mock-axios-adapter'; +import { stubLocation } from '../../testing/stub-location'; describe('wholesalersAxios', () => { describeScopedAxiosContract(() => wholesalersAxios, wholesalersAuthService, '/wholesalers/login'); + + describe('on the password recovery pages', () => { + const realLocation = window.location; + + afterEach(() => { + Object.defineProperty(window, 'location', { value: realLocation, writable: true, configurable: true }); + vi.restoreAllMocks(); + }); + + test.each(['/wholesalers/forgot-password', '/wholesalers/reset-password'])( + 'when a stale token gets a 401 on %s, then it does not log out, notify or redirect away', + async (pathname) => { + vi.spyOn(wholesalersAuthService, 'getToken').mockReturnValue('stale-token'); + const logOutSpy = vi.spyOn(wholesalersAuthService, 'logOut').mockImplementation(() => {}); + const notifySpy = vi.spyOn(notificationsService, 'error').mockImplementation(() => {}); + stubLocation(pathname); + wholesalersAxios.defaults.adapter = mockAdapter(() => ({ status: 401 })); + + await expect(wholesalersAxios.get('/wholesalers/me')).rejects.toMatchObject({ isAxiosError: true }); + + expect(logOutSpy).not.toHaveBeenCalled(); + expect(notifySpy).not.toHaveBeenCalled(); + expect(window.location.href).toBe(''); + }, + ); + }); }); diff --git a/src/wholesalers/core/wholesalers-axios.ts b/src/wholesalers/core/wholesalers-axios.ts index a0378611..e6c688ab 100644 --- a/src/wholesalers/core/wholesalers-axios.ts +++ b/src/wholesalers/core/wholesalers-axios.ts @@ -1,6 +1,7 @@ import axios from 'axios'; import { wholesalersAuthService } from '../services/wholesalers-auth.service'; import notificationsService from '../../services/notifications.service'; +import { isPublicAuthPage } from '../../utils/publicAuthPage'; const wholesalersAxios = axios.create({ baseURL: import.meta.env.VITE_OBJECT_STORAGE_API_URL, @@ -17,7 +18,7 @@ wholesalersAxios.interceptors.request.use((config) => { wholesalersAxios.interceptors.response.use( (response) => response, (error) => { - if (error.response?.status === 401 && !window.location.pathname.endsWith('/login')) { + if (error.response?.status === 401 && !isPublicAuthPage(window.location.pathname)) { wholesalersAuthService.logOut(); notificationsService.error({ text: 'Session expired. Please log in again.' }); window.location.href = '/wholesalers/login'; diff --git a/src/wholesalers/services/wholesalers-auth.service.test.ts b/src/wholesalers/services/wholesalers-auth.service.test.ts index 9a49b2b6..b0f92345 100644 --- a/src/wholesalers/services/wholesalers-auth.service.test.ts +++ b/src/wholesalers/services/wholesalers-auth.service.test.ts @@ -65,6 +65,63 @@ describe('wholesalersAuthService', () => { }); }); + describe('requestPasswordReset', () => { + test('when requestPasswordReset is called, then it posts the email to the forgot-password endpoint', async () => { + let seenConfig: { url?: string; data?: string } | undefined; + wholesalersAxios.defaults.adapter = mockAdapter((config) => { + seenConfig = config; + return { status: 204 }; + }); + + await wholesalersAuthService.requestPasswordReset('wholesaler@internxt.com'); + + expect(seenConfig?.url).toBe('/wholesalers/forgot-password'); + expect(JSON.parse(seenConfig?.data ?? '{}')).toEqual({ email: 'wholesaler@internxt.com' }); + }); + + test('when the backend rejects the captcha, then it maps that into a CaptchaUnavailableError', async () => { + wholesalersAxios.defaults.adapter = mockAdapter(() => ({ + status: 403, + data: { code: 'CAPTCHA_FAILED' }, + })); + + const error = await wholesalersAuthService.requestPasswordReset('wholesaler@internxt.com').catch((e) => e); + + expect(error.name).toBe('CaptchaUnavailableError'); + }); + + test('when the backend rejects for any other reason, then requestPasswordReset rethrows the error untouched', async () => { + wholesalersAxios.defaults.adapter = mockAdapter(() => ({ status: 429 })); + + await expect(wholesalersAuthService.requestPasswordReset('wholesaler@internxt.com')).rejects.toMatchObject({ + isAxiosError: true, + }); + }); + }); + + describe('resetPassword', () => { + test('when resetPassword is called, then it posts the reset token and the new password', async () => { + let seenConfig: { url?: string; data?: string } | undefined; + wholesalersAxios.defaults.adapter = mockAdapter((config) => { + seenConfig = config; + return { status: 204 }; + }); + + await wholesalersAuthService.resetPassword('reset-token', 'new-secret'); + + expect(seenConfig?.url).toBe('/wholesalers/reset-password'); + expect(JSON.parse(seenConfig?.data ?? '{}')).toEqual({ token: 'reset-token', newPassword: 'new-secret' }); + }); + + test('when the reset token is invalid or expired, then resetPassword rethrows the 400', async () => { + wholesalersAxios.defaults.adapter = mockAdapter(() => ({ status: 400 })); + + await expect(wholesalersAuthService.resetPassword('stale-token', 'new-secret')).rejects.toMatchObject({ + response: { status: 400 }, + }); + }); + }); + describe('token management', () => { test('when no one is logged in, then getToken returns null', () => { expect(wholesalersAuthService.getToken()).toBeNull(); diff --git a/src/wholesalers/services/wholesalers-auth.service.ts b/src/wholesalers/services/wholesalers-auth.service.ts index ec5f42df..b454bae3 100644 --- a/src/wholesalers/services/wholesalers-auth.service.ts +++ b/src/wholesalers/services/wholesalers-auth.service.ts @@ -1,4 +1,5 @@ import wholesalersAxios from '../core/wholesalers-axios'; +import { CaptchaUnavailableError, captchaService } from '../../services/captcha.service'; const WHOLESALERS_TOKEN_KEY = 'wholesalersToken'; @@ -23,6 +24,21 @@ async function logIn(email: string, password: string, code?: string): Promise<{ return { twoFactorSetupRequired: !!response.data.twoFactorSetupRequired }; } +async function requestPasswordReset(email: string): Promise { + const captchaHeaders = await captchaService.tryGetHeaders('ForgotPassword'); + + try { + await wholesalersAxios.post('/wholesalers/forgot-password', { email }, { headers: captchaHeaders }); + } catch (err) { + if (captchaService.isCaptchaRejection(err)) throw new CaptchaUnavailableError(); + throw err; + } +} + +async function resetPassword(token: string, newPassword: string): Promise { + await wholesalersAxios.post('/wholesalers/reset-password', { token, newPassword }); +} + function logOut(): void { removeToken(); } @@ -48,6 +64,8 @@ function getRole(): 'wholesaler' | 'member' | null { export const wholesalersAuthService = { getRole, logIn, + requestPasswordReset, + resetPassword, setToken, logOut, getToken, diff --git a/src/wholesalers/views/WholesalersForgotPasswordPage.tsx b/src/wholesalers/views/WholesalersForgotPasswordPage.tsx new file mode 100644 index 00000000..6678f01f --- /dev/null +++ b/src/wholesalers/views/WholesalersForgotPasswordPage.tsx @@ -0,0 +1,13 @@ +import { ForgotPasswordView } from '../../components/auth/ForgotPasswordView'; +import { wholesalersAuthService } from '../services/wholesalers-auth.service'; +import { wholesalersConsoleBranding } from '../console-branding'; + +export const WholesalersForgotPasswordPage = () => { + return ( + + ); +}; diff --git a/src/wholesalers/views/WholesalersLoginPage.tsx b/src/wholesalers/views/WholesalersLoginPage.tsx index 495db6cf..cf5af3f2 100644 --- a/src/wholesalers/views/WholesalersLoginPage.tsx +++ b/src/wholesalers/views/WholesalersLoginPage.tsx @@ -1,25 +1,18 @@ import { LoginPageView } from '../../components/auth/LoginPageView'; import { useWholesalers } from '../context/useWholesalers'; +import { wholesalersConsoleBranding } from '../console-branding'; export const WholesalersLoginPage = () => { const { isAuthenticated, logIn } = useWholesalers(); return ( - Object Storage -
- Wholesalers - - } - rightDescription="Create partners, monitor their storage usage, and manage aggregated billing from one place." - rightFeaturePills={['Partner management', 'Usage monitoring', 'Aggregated billing']} + {...wholesalersConsoleBranding} isAuthenticated={isAuthenticated} logIn={logIn} supportsTwoFactor redirectTo="/wholesalers/partners" + forgotPasswordPath="/wholesalers/forgot-password" /> ); }; diff --git a/src/wholesalers/views/WholesalersResetPasswordPage.tsx b/src/wholesalers/views/WholesalersResetPasswordPage.tsx new file mode 100644 index 00000000..82b3386b --- /dev/null +++ b/src/wholesalers/views/WholesalersResetPasswordPage.tsx @@ -0,0 +1,22 @@ +import { ResetPasswordView } from '../../components/auth/ResetPasswordView'; +import { wholesalersAuthService } from '../services/wholesalers-auth.service'; +import { wholesalersConsoleBranding } from '../console-branding'; +import { useWholesalers } from '../context/useWholesalers'; + +export const WholesalersResetPasswordPage = () => { + const { logOut } = useWholesalers(); + + const resetPassword = async (token: string, newPassword: string) => { + await wholesalersAuthService.resetPassword(token, newPassword); + logOut(); + }; + + return ( + + ); +};