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

Update login step to show message about needing to reset password #578

Merged
merged 1 commit into from
Dec 22, 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
20 changes: 16 additions & 4 deletions src/pages/api/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ const log = logger.child({}, { msgPrefix: '[api/login] ' });

export interface ILoginResponse {
success?: boolean;
error?: 'invalid-credentials' | 'login-failed' | 'failed-userdata-request' | 'invalid-token' | 'method-not-allowed';
error?:
| 'invalid-credentials'
| 'login-failed'
| 'failed-userdata-request'
| 'invalid-token'
| 'method-not-allowed'
| 'must-reset-credentials';
}

export default withIronSessionApiRoute(login, sessionConfig);
Expand Down Expand Up @@ -100,10 +106,16 @@ export const handleAuthentication = async (
return res.status(200).json({ success: false, error: 'failed-userdata-request' });
}
}
log.debug('Login failed', { data });
log.debug({ data }, 'Login failed');
return res.status(401).json({ success: false, error: 'login-failed' });
} catch (error) {
log.trace('Login failed', { error });
} catch (err) {
log.error({ err }, 'Login failed');

// if the login failed due to a password reset requirement, return a specific error
if (axios.isAxiosError(err) && err.response && err.response.status === HttpStatusCode.UnprocessableEntity) {
return res.status(401).json({ success: false, error: 'must-reset-credentials' });
}

return res.status(401).json({ success: false, error: 'login-failed' });
}
};
11 changes: 10 additions & 1 deletion src/pages/user/account/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const Login: NextPage = () => {
['login'],
async (params) => {
const { data } = await axios.post<ILoginResponse>('/api/auth/login', params);

if (data?.error) {
throw new Error(data.error);
}
Expand Down Expand Up @@ -69,7 +70,7 @@ const Login: NextPage = () => {
if (isError) {
focus();
}
}, [isError]);
}, [isError, focus]);

return (
<>
Expand Down Expand Up @@ -143,6 +144,14 @@ const LoginErrorMessage = (props: { error: AxiosError<ILoginResponse> | Error })
case 'failed-userdata-request':
case 'invalid-token':
return <StandardAlertMessage status="error" title="Unable to login" description="Please try again later." />;
case 'must-reset-credentials':
return (
<StandardAlertMessage
status="error"
title="Please reset your password"
description="Your password does not meet the new security requirements. An email has been sent to you with instructions on how to reset your password."
/>
);
default:
return null;
}
Expand Down
Loading