From 24949bce67ede7084af8d58b734d66a6387ecbd8 Mon Sep 17 00:00:00 2001 From: jzunigax2 <125698953+jzunigax2@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:31:57 -0600 Subject: [PATCH 1/2] feat: implement password reset functionality for wholesalers - Added WholesalersForgotPasswordPage and WholesalersResetPasswordPage components for handling password reset requests. - Integrated requestPasswordReset and resetPassword methods in wholesalersAuthService to manage API interactions. - Updated WholesalersLoginPage to include a link to the forgot password page. - Created console branding for consistent UI across wholesaler authentication views. - Enhanced unit tests for wholesalersAuthService to cover new password reset functionalities. --- src/App.tsx | 4 ++ src/wholesalers/console-branding.tsx | 12 ++++ .../services/wholesalers-auth.service.test.ts | 57 +++++++++++++++++++ .../services/wholesalers-auth.service.ts | 18 ++++++ .../views/WholesalersForgotPasswordPage.tsx | 13 +++++ .../views/WholesalersLoginPage.tsx | 13 +---- .../views/WholesalersResetPasswordPage.tsx | 14 +++++ 7 files changed, 121 insertions(+), 10 deletions(-) create mode 100644 src/wholesalers/console-branding.tsx create mode 100644 src/wholesalers/views/WholesalersForgotPasswordPage.tsx create mode 100644 src/wholesalers/views/WholesalersResetPasswordPage.tsx 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/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/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..daf49481 --- /dev/null +++ b/src/wholesalers/views/WholesalersResetPasswordPage.tsx @@ -0,0 +1,14 @@ +import { ResetPasswordView } from '../../components/auth/ResetPasswordView'; +import { wholesalersAuthService } from '../services/wholesalers-auth.service'; +import { wholesalersConsoleBranding } from '../console-branding'; + +export const WholesalersResetPasswordPage = () => { + return ( + + ); +}; From b1681ddf3c75ce82e5463f06475c2dd9fecb5bf1 Mon Sep 17 00:00:00 2001 From: jzunigax2 <125698953+jzunigax2@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:45:13 -0600 Subject: [PATCH 2/2] feat: implement public authentication page detection and related tests - Added isPublicAuthPage utility function to determine if a given pathname corresponds to a public authentication page. - Created unit tests for isPublicAuthPage to validate its functionality against various paths. - Updated wholesalersAxios to utilize isPublicAuthPage for handling 401 errors more effectively. - Modified WholesalersResetPasswordPage to integrate the resetPassword function with logout functionality. --- src/utils/publicAuthPage.test.ts | 18 +++++++++++ src/utils/publicAuthPage.ts | 5 +++ .../core/wholesalers-axios.test.ts | 31 ++++++++++++++++++- src/wholesalers/core/wholesalers-axios.ts | 3 +- .../views/WholesalersResetPasswordPage.tsx | 10 +++++- 5 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 src/utils/publicAuthPage.test.ts create mode 100644 src/utils/publicAuthPage.ts 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/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/views/WholesalersResetPasswordPage.tsx b/src/wholesalers/views/WholesalersResetPasswordPage.tsx index daf49481..82b3386b 100644 --- a/src/wholesalers/views/WholesalersResetPasswordPage.tsx +++ b/src/wholesalers/views/WholesalersResetPasswordPage.tsx @@ -1,14 +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 ( ); };