Backend error messages for signup/login can now be interpreted by locale
Fixed a bad err check on backend
This commit is contained in:
parent
b589feae8d
commit
8a6a355097
5 changed files with 25 additions and 9 deletions
|
@ -4,6 +4,8 @@ import (
|
|||
"encoding/json"
|
||||
"net/http"
|
||||
h "npm/internal/api/http"
|
||||
"npm/internal/errors"
|
||||
"npm/internal/logger"
|
||||
|
||||
c "npm/internal/api/context"
|
||||
"npm/internal/entity/auth"
|
||||
|
@ -35,21 +37,24 @@ func NewToken() func(http.ResponseWriter, *http.Request) {
|
|||
// Find user
|
||||
userObj, userErr := user.GetByEmail(payload.Identity)
|
||||
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
|
||||
}
|
||||
|
||||
// Get Auth
|
||||
authObj, authErr := auth.GetByUserIDType(userObj.ID, payload.Type)
|
||||
if userErr != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, authErr.Error(), nil)
|
||||
if authErr != nil {
|
||||
logger.Debug("%s: %s", errors.ErrInvalidLogin.Error(), authErr.Error())
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, errors.ErrInvalidLogin.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify Auth
|
||||
validateErr := authObj.ValidateSecret(payload.Secret)
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import "errors"
|
|||
// All error messages used by the service package to report
|
||||
// problems back to calling clients
|
||||
var (
|
||||
ErrInvalidLogin = errors.New("invalid_login_credentials")
|
||||
ErrDatabaseUnavailable = errors.New("Database is unavailable")
|
||||
ErrDuplicateEmailUser = errors.New("A user already exists with this email address")
|
||||
)
|
||||
|
|
|
@ -32,6 +32,12 @@
|
|||
"column.wildcard_support": {
|
||||
"defaultMessage": "Wildcard Support"
|
||||
},
|
||||
"error.cannot_create_user": {
|
||||
"defaultMessage": "Cannot create user"
|
||||
},
|
||||
"error.invalid_login_credentials": {
|
||||
"defaultMessage": "Invalid login credentials"
|
||||
},
|
||||
"footer.changelog": {
|
||||
"defaultMessage": "Change Log"
|
||||
},
|
||||
|
|
|
@ -37,8 +37,10 @@ function Login() {
|
|||
await login(formData.email, formData.password);
|
||||
} catch (err: any) {
|
||||
toast({
|
||||
title: "Login Error",
|
||||
description: err.message,
|
||||
description: intl.formatMessage({
|
||||
id: `error.${err.message}`,
|
||||
defaultMessage: err.message,
|
||||
}),
|
||||
status: "error",
|
||||
position: "top",
|
||||
duration: 3000,
|
||||
|
|
|
@ -51,8 +51,10 @@ function Setup() {
|
|||
|
||||
const showErr = (msg: string) => {
|
||||
toast({
|
||||
title: "Signup Error",
|
||||
description: msg,
|
||||
description: intl.formatMessage({
|
||||
id: `error.${msg}`,
|
||||
defaultMessage: msg,
|
||||
}),
|
||||
status: "error",
|
||||
position: "top",
|
||||
duration: 3000,
|
||||
|
@ -74,7 +76,7 @@ function Setup() {
|
|||
setLoading(false);
|
||||
}
|
||||
} else {
|
||||
showErr("Unable to create user!");
|
||||
showErr("cannot_create_user");
|
||||
}
|
||||
} catch (err: any) {
|
||||
showErr(err.message);
|
||||
|
|
Loading…
Reference in a new issue