Backend error messages for signup/login can now be interpreted by locale

Fixed a bad err check on backend
This commit is contained in:
Jamie Curnow 2021-11-09 10:49:35 +10:00
parent b589feae8d
commit 8a6a355097
5 changed files with 25 additions and 9 deletions

View file

@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
h "npm/internal/api/http" h "npm/internal/api/http"
"npm/internal/errors"
"npm/internal/logger"
c "npm/internal/api/context" c "npm/internal/api/context"
"npm/internal/entity/auth" "npm/internal/entity/auth"
@ -35,21 +37,24 @@ func NewToken() func(http.ResponseWriter, *http.Request) {
// Find user // Find user
userObj, userErr := user.GetByEmail(payload.Identity) userObj, userErr := user.GetByEmail(payload.Identity)
if userErr != nil { if userErr != nil {
h.ResultErrorJSON(w, r, http.StatusBadRequest, userErr.Error(), nil) logger.Debug("%s: %s", errors.ErrInvalidLogin.Error(), userErr.Error())
h.ResultErrorJSON(w, r, http.StatusBadRequest, errors.ErrInvalidLogin.Error(), nil)
return return
} }
// Get Auth // Get Auth
authObj, authErr := auth.GetByUserIDType(userObj.ID, payload.Type) authObj, authErr := auth.GetByUserIDType(userObj.ID, payload.Type)
if userErr != nil { if authErr != nil {
h.ResultErrorJSON(w, r, http.StatusBadRequest, authErr.Error(), nil) logger.Debug("%s: %s", errors.ErrInvalidLogin.Error(), authErr.Error())
h.ResultErrorJSON(w, r, http.StatusBadRequest, errors.ErrInvalidLogin.Error(), nil)
return return
} }
// Verify Auth // Verify Auth
validateErr := authObj.ValidateSecret(payload.Secret) validateErr := authObj.ValidateSecret(payload.Secret)
if validateErr != nil { if validateErr != nil {
h.ResultErrorJSON(w, r, http.StatusBadRequest, validateErr.Error(), nil) logger.Debug("%s: %s", errors.ErrInvalidLogin.Error(), validateErr.Error())
h.ResultErrorJSON(w, r, http.StatusBadRequest, errors.ErrInvalidLogin.Error(), nil)
return return
} }

View file

@ -5,6 +5,7 @@ import "errors"
// All error messages used by the service package to report // All error messages used by the service package to report
// problems back to calling clients // problems back to calling clients
var ( var (
ErrInvalidLogin = errors.New("invalid_login_credentials")
ErrDatabaseUnavailable = errors.New("Database is unavailable") ErrDatabaseUnavailable = errors.New("Database is unavailable")
ErrDuplicateEmailUser = errors.New("A user already exists with this email address") ErrDuplicateEmailUser = errors.New("A user already exists with this email address")
) )

View file

@ -32,6 +32,12 @@
"column.wildcard_support": { "column.wildcard_support": {
"defaultMessage": "Wildcard Support" "defaultMessage": "Wildcard Support"
}, },
"error.cannot_create_user": {
"defaultMessage": "Cannot create user"
},
"error.invalid_login_credentials": {
"defaultMessage": "Invalid login credentials"
},
"footer.changelog": { "footer.changelog": {
"defaultMessage": "Change Log" "defaultMessage": "Change Log"
}, },

View file

@ -37,8 +37,10 @@ function Login() {
await login(formData.email, formData.password); await login(formData.email, formData.password);
} catch (err: any) { } catch (err: any) {
toast({ toast({
title: "Login Error", description: intl.formatMessage({
description: err.message, id: `error.${err.message}`,
defaultMessage: err.message,
}),
status: "error", status: "error",
position: "top", position: "top",
duration: 3000, duration: 3000,

View file

@ -51,8 +51,10 @@ function Setup() {
const showErr = (msg: string) => { const showErr = (msg: string) => {
toast({ toast({
title: "Signup Error", description: intl.formatMessage({
description: msg, id: `error.${msg}`,
defaultMessage: msg,
}),
status: "error", status: "error",
position: "top", position: "top",
duration: 3000, duration: 3000,
@ -74,7 +76,7 @@ function Setup() {
setLoading(false); setLoading(false);
} }
} else { } else {
showErr("Unable to create user!"); showErr("cannot_create_user");
} }
} catch (err: any) { } catch (err: any) {
showErr(err.message); showErr(err.message);