SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
feat(auth): validate reset password token on page load before showing form (#11522)
SagarRajput-7fb0d34ae3598
diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.tsindex 27e937848e..f1c8cf67d6 100644--- a/frontend/src/api/index.ts+++ b/frontend/src/api/index.ts@@ -120,7 +120,8 @@ export const interceptorRejected = async (!(response.config.url === '/sessions' && response.config.method === 'delete') &&- response.config.url !== '/authz/check'+ response.config.url !== '/authz/check' &&+ response.config.url !== '/api/v2/reset_password_tokens/verify') {try {const accessToken = getLocalStorageApi(LOCALSTORAGE.AUTH_TOKEN);diff --git a/frontend/src/container/ResetPassword/ResetPassword.styles.scss b/frontend/src/container/ResetPassword/ResetPassword.styles.scssindex 5aba891a6f..92d660b0f5 100644--- a/frontend/src/container/ResetPassword/ResetPassword.styles.scss+++ b/frontend/src/container/ResetPassword/ResetPassword.styles.scss@@ -21,6 +21,10 @@justify-content: center;margin-bottom: 8px;color: var(--semantic-primary-foreground);++ &--error {+ color: var(--destructive);+ }}.reset-password-header-title {diff --git a/frontend/src/container/ResetPassword/TokenError.tsx b/frontend/src/container/ResetPassword/TokenError.tsxnew file mode 100644index 0000000000..fc9fe5862c--- /dev/null+++ b/frontend/src/container/ResetPassword/TokenError.tsx@@ -0,0 +1,67 @@+import { CircleAlert } from '@signozhq/icons';+import { Typography } from '@signozhq/ui/typography';+import AuthError from 'components/AuthError/AuthError';+import AuthPageContainer from 'components/AuthPageContainer';+import APIError from 'types/api/error';++import './ResetPassword.styles.scss';++interface TokenErrorContent {+ title: string;+ subtitle: string;+}++function getErrorContent(error?: APIError): TokenErrorContent {+ const code = error?.getErrorCode();++ if (code === 'reset_password_token_expired') {+ return {+ title: 'Reset Password token is expired',+ subtitle:+ 'Password reset links are single-use and expire after a set period. Please request a new password reset link.',+ };+ }++ if (code === 'reset_password_token_not_found') {+ return {+ title: 'Invalid Reset Link',+ subtitle:+ 'This reset password link is invalid or has already been used. Please request a new password reset link.',+ };+ }++ return {+ title: 'Reset Link Unavailable',+ subtitle:+ 'We could not validate your reset password link. Please request a new one.',+ };+}++interface TokenErrorProps {+ error?: APIError;+}++function TokenError({ error }: TokenErrorProps): JSX.Element {+ const { title, subtitle } = getErrorContent(error);++ return (+ <AuthPageContainer>+ <div className="reset-password-card reset-password-card--centered">+ <div className="reset-password-header">+ <div className="reset-password-header-icon reset-password-header-icon--error">+ <CircleAlert size={32} />+ </div>+ <Typography.Title level={4} className="reset-password-header-title">+ {title}+ </Typography.Title>+ <Typography.Text className="reset-password-header-subtitle">+ {subtitle}+ </Typography.Text>+ </div>+ {error && <AuthError error={error} />}+ </div>+ </AuthPageContainer>+ );+}++export default TokenError;diff --git a/frontend/src/container/ResetPassword/__tests__/ResetPassword.test.tsx b/frontend/src/container/ResetPassword/__tests__/ResetPassword.test.tsxindex 1446728816..6c072d6fe6 100644--- a/frontend/src/container/ResetPassword/__tests__/ResetPassword.test.tsx+++ b/frontend/src/container/ResetPassword/__tests__/ResetPassword.test.tsx@@ -1,4 +1,3 @@-import { Logout } from 'api/utils';import ROUTES from 'constants/routes';import history from 'lib/history';import { rest, server } from 'mocks-server/server';@@ -17,10 +16,6 @@ jest.mock('lib/history', () => ({},}));-jest.mock('api/utils', () => ({- Logout: jest.fn(),-}));-const mockSuccessNotification = jest.fn();const mockErrorNotification = jest.fn();@@ -70,17 +65,6 @@ describe('ResetPassword Component', () => {).toBeInTheDocument();expect(screen.getByText(/signoz 1\.0\.0/i)).toBeInTheDocument();});-- it('redirects to login when token is missing', () => {- window.history.pushState({}, '', '/password-reset');-- render(<ResetPassword version="1.0.0" />, undefined, {- initialRoute: '/password-reset',- });-- expect(Logout).toHaveBeenCalled();- expect(mockHistoryPush).toHaveBeenCalledWith(ROUTES.LOGIN);- });});describe('Form Validation', () => {diff --git a/frontend/src/container/ResetPassword/index.tsx b/frontend/src/container/ResetPassword/index.tsxindex 2fb117c236..cdefd21bc4 100644--- a/frontend/src/container/ResetPassword/index.tsx+++ b/frontend/src/container/ResetPassword/index.tsx@@ -1,11 +1,10 @@-import { useEffect, useState } from 'react';+import { useState } from 'react';import { useTranslation } from 'react-i18next';import { useLocation } from 'react-use';import { Button } from '@signozhq/ui/button';import { Callout } from '@signozhq/ui/callout';import { Form, Input as AntdInput } from 'antd';import { Typography } from '@signozhq/ui/typography';-import { Logout } from 'api/utils';import resetPasswordApi from 'api/v1/factor_password/resetPassword';import AuthError from 'components/AuthError/AuthError';import AuthPageContainer from 'components/AuthPageContainer';@@ -38,13 +37,6 @@ function ResetPassword({ version }: ResetPasswordProps): JSX.Element {const { notifications } = useNotifications();const [form] = Form.useForm<FormValues>();- useEffect(() => {- if (!token) {- Logout();- history.push(ROUTES.LOGIN);- }- }, [token]);-const handleFormSubmit: () => Promise<void> = async () => {try {setLoading(true);diff --git a/frontend/src/pages/ResetPassword/__tests__/ResetPasswordPage.test.tsx b/frontend/src/pages/ResetPassword/__tests__/ResetPasswordPage.test.tsxnew file mode 100644index 0000000000..b55b06a173--- /dev/null+++ b/frontend/src/pages/ResetPassword/__tests__/ResetPasswordPage.test.tsx@@ -0,0 +1,155 @@+import { Logout } from 'api/utils';+import ROUTES from 'constants/routes';+import history from 'lib/history';+import { createErrorResponse, rest, server } from 'mocks-server/server';+import { render, screen, waitFor } from 'tests/test-utils';++import ResetPassword from '../index';++jest.mock('lib/history', () => ({+ __esModule: true,+ default: {+ push: jest.fn(),+ location: { search: '' },+ },+}));++jest.mock('api/utils', () => ({+ Logout: jest.fn().mockResolvedValue(undefined),+}));++const VERIFY_TOKEN_ENDPOINT = '*/api/v2/reset_password_tokens/verify';+const VERSION_ENDPOINT = '*/version';++const mockHistoryPush = history.push as jest.MockedFunction<+ typeof history.push+>;++const successVerifyResponse = {+ data: { id: 'token-id', token: 'valid-token' },+};++const successVersionResponse = {+ version: '0.0.1',+ ee: 'Y',+ setupCompleted: true,+};++describe('ResetPassword Page', () => {+ beforeEach(() => {+ jest.clearAllMocks();+ server.use(+ rest.get(VERSION_ENDPOINT, (_, res, ctx) =>+ res(ctx.status(200), ctx.json(successVersionResponse)),+ ),+ );+ });++ afterEach(() => {+ server.resetHandlers();+ });++ describe('Token validation on page load', () => {+ it('shows spinner then form when token is valid', async () => {+ server.use(+ rest.post(VERIFY_TOKEN_ENDPOINT, (_, res, ctx) =>+ res(ctx.delay(50), ctx.status(200), ctx.json(successVerifyResponse)),+ ),+ );++ window.history.pushState({}, '', '/password-reset?token=valid-token');+ render(<ResetPassword />, undefined, {+ initialRoute: '/password-reset?token=valid-token',+ });++ // Loading state: spinner visible, form and error absent+ expect(screen.getByRole('img', { name: /loading/i })).toBeInTheDocument();+ expect(screen.queryByTestId('password')).not.toBeInTheDocument();+ expect(+ screen.queryByText(/reset password token is expired/i),+ ).not.toBeInTheDocument();++ // After verification resolves: form is shown+ await waitFor(() => {+ expect(screen.getByTestId('password')).toBeInTheDocument();+ });+ expect(screen.getByTestId('confirmPassword')).toBeInTheDocument();+ });++ it('shows "Invalid Reset Link" when token is not found (404)', async () => {+ server.use(+ rest.post(+ VERIFY_TOKEN_ENDPOINT,+ createErrorResponse(+ 404,+ 'reset_password_token_not_found',+ 'reset password token does not exist',+ ),+ ),+ );++ window.history.pushState({}, '', '/password-reset?token=invalid-token');+ render(<ResetPassword />, undefined, {+ initialRoute: '/password-reset?token=invalid-token',+ });++ await waitFor(() => {+ expect(screen.getByText(/invalid reset link/i)).toBeInTheDocument();+ });++ expect(+ screen.getByText(/invalid or has already been used/i),+ ).toBeInTheDocument();+ expect(+ screen.getByText(/reset password token does not exist/i),+ ).toBeInTheDocument();+ });++ it('shows "token is expired" when token is expired (401) without redirecting to login', async () => {+ server.use(+ rest.post(+ VERIFY_TOKEN_ENDPOINT,+ createErrorResponse(+ 401,+ 'reset_password_token_expired',+ 'reset password token has expired',+ ),+ ),+ );++ window.history.pushState({}, '', '/password-reset?token=expired-token');+ render(<ResetPassword />, undefined, {+ initialRoute: '/password-reset?token=expired-token',+ });++ await waitFor(() => {+ expect(+ screen.getByText(/reset password token is expired/i),+ ).toBeInTheDocument();+ });++ expect(+ screen.getByText(/single-use and expire after a set period/i),+ ).toBeInTheDocument();+ expect(+ screen.getByText(/reset password token has expired/i),+ ).toBeInTheDocument();+ // 401 from this endpoint must NOT trigger logout/redirect+ expect(mockHistoryPush).not.toHaveBeenCalledWith(ROUTES.LOGIN);+ expect(Logout).not.toHaveBeenCalled();+ });++ it('redirects to login when no token is in the URL', async () => {+ window.history.pushState({}, '', '/password-reset');+ render(<ResetPassword />, undefined, {+ initialRoute: '/password-reset',+ });++ await waitFor(() => {+ expect(mockHistoryPush).toHaveBeenCalledWith(ROUTES.LOGIN);+ });++ expect(Logout).toHaveBeenCalled();+ });+ });+});diff --git a/frontend/src/pages/ResetPassword/index.tsx b/frontend/src/pages/ResetPassword/index.tsxindex 6395a4e01d..84aa5eb194 100644--- a/frontend/src/pages/ResetPassword/index.tsx+++ b/frontend/src/pages/ResetPassword/index.tsx@@ -1,8 +1,17 @@-import { useEffect } from 'react';+import { useEffect, useMemo } from 'react';import { useQuery } from 'react-query';+import { useLocation } from 'react-use';+import { AxiosError } from 'axios';+import { convertToApiError } from 'api/ErrorResponseHandlerForGeneratedAPIs';import getUserVersion from 'api/v1/version/get';+import { verifyResetPasswordToken } from 'api/generated/services/users';+import { RenderErrorResponseDTO } from 'api/generated/services/sigNoz.schemas';+import { Logout } from 'api/utils';import Spinner from 'components/Spinner';import ResetPasswordContainer from 'container/ResetPassword';+import TokenError from 'container/ResetPassword/TokenError';+import ROUTES from 'constants/routes';+import history from 'lib/history';import { useAppContext } from 'providers/App/App';import { useErrorModal } from 'providers/ErrorModalProvider';import APIError from 'types/api/error';@@ -10,24 +19,65 @@ import APIError from 'types/api/error';function ResetPassword(): JSX.Element {const { user, isLoggedIn } = useAppContext();const { showErrorModal } = useErrorModal();+ const { search } = useLocation();+ const params = new URLSearchParams(search || '');+ const token = params.get('token') || '';- const { data, isLoading, error } = useQuery({+ useEffect(() => {+ if (!token) {+ void Logout();+ history.push(ROUTES.LOGIN);+ }+ }, [token]);++ const {+ data: versionData,+ isLoading: isVersionLoading,+ error: versionError,+ } = useQuery({queryFn: getUserVersion,queryKey: ['getUserVersion', user?.accessJwt],enabled: !isLoggedIn,});+ const {+ isLoading: isVerifying,+ isError: isTokenError,+ error: tokenError,+ } = useQuery<+ Awaited<ReturnType<typeof verifyResetPasswordToken>>,+ AxiosError<RenderErrorResponseDTO>+ >({+ queryFn: () => verifyResetPasswordToken({ token }),+ queryKey: ['verifyResetPasswordToken', token],+ enabled: !!token,+ retry: false,+ });++ const tokenApiError = useMemo(+ () => convertToApiError(tokenError),+ [tokenError],+ );+useEffect(() => {- if (error) {- showErrorModal(error as APIError);+ if (versionError) {+ showErrorModal(versionError as APIError);}- }, [error, showErrorModal]);+ }, [versionError, showErrorModal]);- if (isLoading) {+ if (!token) {return <Spinner tip="Loading..." />;}- return <ResetPasswordContainer version={data?.data.version || ''} />;+ if (isVersionLoading || isVerifying) {+ return <Spinner tip="Validating your reset password token..." />;+ }++ if (isTokenError) {+ return <TokenError error={tokenApiError} />;+ }++ return <ResetPasswordContainer version={versionData?.data.version || ''} />;}export default ResetPassword;