refactor(frontend): error handling in the auth/local api route

This now uses the new error code for a disabled registration (403) and also handles error where the password is too weak (400).

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2023-01-08 16:11:41 +01:00 committed by David Mehren
parent 0ec9edc07d
commit 45e70434c4
4 changed files with 11 additions and 5 deletions

View file

@ -561,7 +561,8 @@
"message": "Your account has been registered successfully. You can now log in with your username and password." "message": "Your account has been registered successfully. You can now log in with your username and password."
}, },
"error": { "error": {
"registrationDisabled": "The registration is disabled", "passwordTooWeak": "The password is too weak.",
"registrationDisabled": "The registration is disabled.",
"usernameExisting": "There is already an account with this username.", "usernameExisting": "There is already an account with this username.",
"other": "There was an error while registering your account. Just try it again." "other": "There was an error while registering your account. Just try it again."
} }

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -36,6 +36,7 @@ export const doLocalLogin = async (username: string, password: string): Promise<
* @param username The username of the new user. * @param username The username of the new user.
* @param displayName The display name of the new user. * @param displayName The display name of the new user.
* @param password The password of the new user. * @param password The password of the new user.
* @throws {RegisterError.PASSWORD_TOO_WEAK} when the backend deems the password too weak.
* @throws {RegisterError.USERNAME_EXISTING} when there is already an existing user with the same username. * @throws {RegisterError.USERNAME_EXISTING} when there is already an existing user with the same username.
* @throws {RegisterError.REGISTRATION_DISABLED} when the registration of local users has been disabled on the backend. * @throws {RegisterError.REGISTRATION_DISABLED} when the registration of local users has been disabled on the backend.
* @throws {Error} when the api request wasn't successful. * @throws {Error} when the api request wasn't successful.
@ -48,7 +49,8 @@ export const doLocalRegister = async (username: string, displayName: string, pas
password password
}) })
.withStatusCodeErrorMapping({ .withStatusCodeErrorMapping({
400: RegisterError.REGISTRATION_DISABLED, 400: RegisterError.PASSWORD_TOO_WEAK,
403: RegisterError.REGISTRATION_DISABLED,
409: RegisterError.USERNAME_EXISTING 409: RegisterError.USERNAME_EXISTING
}) })
.sendRequest() .sendRequest()

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -12,6 +12,7 @@ export enum AuthError {
export enum RegisterError { export enum RegisterError {
USERNAME_EXISTING = 'usernameExisting', USERNAME_EXISTING = 'usernameExisting',
PASSWORD_TOO_WEAK = 'passwordTooWeak',
REGISTRATION_DISABLED = 'registrationDisabled', REGISTRATION_DISABLED = 'registrationDisabled',
OTHER = 'other' OTHER = 'other'
} }

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -22,6 +22,8 @@ export const RegisterError: React.FC<RegisterErrorProps> = ({ error }) => {
const errorMessageI18nKey = useMemo(() => { const errorMessageI18nKey = useMemo(() => {
switch (error) { switch (error) {
case RegisterErrorType.PASSWORD_TOO_WEAK:
return 'login.register.error.passwordTooWeak'
case RegisterErrorType.REGISTRATION_DISABLED: case RegisterErrorType.REGISTRATION_DISABLED:
return 'login.register.error.registrationDisabled' return 'login.register.error.registrationDisabled'
case RegisterErrorType.USERNAME_EXISTING: case RegisterErrorType.USERNAME_EXISTING: