accounts-frontend/packages/app/services/errorsDict/errorsDict.tsx
ErickSkrauch be08857edc
Add E2E tests for device code grant flow.
Handle more errors for device code.
Dispatch a BSOD for an any unhandled exception from auth flow state
2024-12-18 01:02:02 +01:00

158 lines
6.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { ComponentType, ReactElement, ReactNode } from 'react';
import { Link } from 'react-router-dom';
import { FormattedMessage as Message } from 'react-intl';
import { RelativeTime } from 'app/components/ui';
const SuggestResetPassword: ComponentType = () => (
<>
<br />
<Message
key="suggestResetPassword"
defaultMessage="Have you {forgotYourPassword}?"
values={{
forgotYourPassword: (
<Link to="/forgot-password">
<Message key="forgotYourPassword" defaultMessage="forgot your password" />
</Link>
),
}}
/>
</>
);
const ResendKey: ComponentType<{ url: string }> = ({ url }) => (
<>
{' '}
<Link to={url}>
<Message key="doYouWantRequestKey" defaultMessage="Do you want to request a new key?" />
</Link>
</>
);
const errorsMap: Record<string, (props?: Record<string, any>) => ReactElement> = {
'error.login_required': () => <Message key="loginRequired" defaultMessage="Please enter Email or username" />,
'error.login_not_exist': () => (
<Message key="loginNotExist" defaultMessage="Sorry, Ely doesn't recognise your login." />
),
'error.password_required': () => <Message key="passwordRequired" defaultMessage="Please enter password" />,
'error.password_incorrect': (props) => (
// props are handled in validationErrorsHandler in components/auth/actions
<>
<Message key="invalidPassword" defaultMessage="You have entered wrong account password." />
{props && props.isGuest ? <SuggestResetPassword /> : null}
</>
),
'error.username_required': () => <Message key="usernameRequired" defaultMessage="Username is required" />,
'error.username_invalid': () => <Message key="usernameInvalid" defaultMessage="Username is invalid" />,
'error.username_too_short': () => <Message key="usernameTooShort" defaultMessage="Username is too short" />,
'error.username_too_long': () => <Message key="usernameTooLong" defaultMessage="Username is too long" />,
'error.username_not_available': () => (
<Message key="usernameUnavailable" defaultMessage="This username is already taken" />
),
'error.email_required': () => <Message key="emailRequired" defaultMessage="Email is required" />,
'error.email_too_long': () => <Message key="emailToLong" defaultMessage="Email is too long" />,
'error.email_invalid': () => <Message key="emailInvalid" defaultMessage="Email is invalid" />,
'error.email_is_tempmail': () => (
<Message key="emailIsTempmail" defaultMessage="Tempmail Email addresses is not allowed" />
),
'error.email_not_available': (props) => (
// props are handled in validationErrorsHandler in components/auth/actions
<>
<Message key="emailNotAvailable" defaultMessage="This Email is already registered." />
{props && props.isGuest ? <SuggestResetPassword /> : null}
</>
),
'error.totp_required': () => <Message key="totpRequired" defaultMessage="Please, enter the code" />,
'error.totp_incorrect': () => <Message key="totpIncorrect" defaultMessage="The code is incorrect" />,
'error.otp_already_enabled': () => (
<Message key="mfaAlreadyEnabled" defaultMessage="The two factor auth is already enabled" />
),
'error.rePassword_required': () => (
<Message key="rePasswordRequired" defaultMessage="Please retype your password" />
),
'error.password_too_short': () => (
<Message key="passwordTooShort" defaultMessage="Your password should be at least 8 characters length" />
),
'error.rePassword_does_not_match': () => (
<Message key="passwordsDoesNotMatch" defaultMessage="The passwords does not match" />
),
'error.rulesAgreement_required': () => (
<Message key="rulesAgreementRequired" defaultMessage="You must accept rules in order to create an account" />
),
'error.key_required': () => <Message key="keyRequired" defaultMessage="Please, enter an activation key" />,
'error.key_not_exists': (props) => (
<>
<Message key="keyNotExists" defaultMessage="The key is incorrect or has expired." />
{props && props.repeatUrl ? <ResendKey url={props.repeatUrl} /> : null}
</>
),
'error.key_expire': (props) => errorsMap['error.key_not_exists'](props),
'error.newPassword_required': () => (
<Message key="newPasswordRequired" defaultMessage="Please enter new password" />
),
'error.newRePassword_required': () => (
<Message key="newRePasswordRequired" defaultMessage="Please repeat new password" />
),
'error.account_not_activated': () => (
<Message key="accountNotActivated" defaultMessage="The account is not activated" />
),
'error.account_banned': () => <Message key="accountBanned" defaultMessage="Account is blocked" />,
'error.recently_sent_message': (props) => (
<Message
key="emailFrequency"
defaultMessage="Please cool down, you are requesting Emails too often. New key can be retrieved {time}."
values={{
// for msLeft @see AuthError.jsx
time: <RelativeTime timestamp={props!.msLeft} />,
}}
/>
),
'error.email_not_found': () => <Message key="emailNotFound" defaultMessage="Specified Email is not found" />,
'error.account_already_activated': () => (
<Message key="accountAlreadyActivated" defaultMessage="This account is already activated" />
),
'error.captcha_required': () => <Message key="captchaRequired" defaultMessage="Please, solve the captcha" />,
'error.captcha_invalid': (props) => errorsMap['error.captcha_required'](props),
'error.redirectUri_required': () => <Message key="redirectUriRequired" defaultMessage="Redirect URI is required" />,
'error.redirectUri_invalid': () => <Message key="redirectUriInvalid" defaultMessage="Redirect URI is invalid" />,
invalid_user_code: () => <Message key="invalidUserCode" defaultMessage="Invalid Device Code" />,
expired_token: () => (
<Message
key="expiredUserCode"
defaultMessage="The code has expired. Start the authorization flow in the application again."
/>
),
used_user_code: () => <Message key="usedUserCode" defaultMessage="This code has been already used" />,
};
interface ErrorLiteral {
type: string;
payload?: Record<string, any>;
}
type Error = string | ErrorLiteral;
export function resolve(error: Error): ReactNode {
let payload = {};
if (typeof error !== 'string') {
payload = error.payload || {};
error = error.type;
}
return errorsMap[error] ? errorsMap[error](payload) : error;
}