add user-friendly error

This commit is contained in:
MaysWind
2021-01-01 23:45:26 +08:00
parent 0211c49f99
commit 102c945aa0
5 changed files with 15 additions and 0 deletions
+2
View File
@@ -151,6 +151,8 @@ func startWebServer(c *cli.Context) error {
if config.EnableUserRegister {
apiRoute.POST("/register.json", bindApi(api.Users.UserRegisterHandler))
} else {
apiRoute.POST("/register.json", bindApi(api.Users.UserRegistrationNotAllowed))
}
apiRoute.GET("/logout.json", bindApi(api.Tokens.TokenRevokeCurrentHandler))
+10
View File
@@ -9,6 +9,7 @@ import (
"github.com/mayswind/lab/pkg/log"
"github.com/mayswind/lab/pkg/models"
"github.com/mayswind/lab/pkg/services"
"github.com/mayswind/lab/pkg/settings"
)
// UsersApi represents user api
@@ -27,6 +28,10 @@ var (
// UserRegisterHandler saves a new user by request parameters
func (a *UsersApi) UserRegisterHandler(c *core.Context) (interface{}, *errs.Error) {
if !settings.Container.Current.EnableUserRegister {
return a.UserRegistrationNotAllowed(c)
}
var userRegisterReq models.UserRegisterRequest
err := c.ShouldBindJSON(&userRegisterReq)
@@ -76,6 +81,11 @@ func (a *UsersApi) UserRegisterHandler(c *core.Context) (interface{}, *errs.Erro
return authResp, nil
}
// UserRegistrationNotAllowed returns user registration not allowed error
func (a *UsersApi) UserRegistrationNotAllowed(c *core.Context) (interface{}, *errs.Error) {
return nil, errs.ErrUserRegistrationNotAllowed
}
// UserProfileHandler returns user profile of current user
func (a *UsersApi) UserProfileHandler(c *core.Context) (interface{}, *errs.Error) {
uid := c.GetCurrentUid()
+1
View File
@@ -17,4 +17,5 @@ var (
ErrUserPasswordWrong = NewNormalError(NormalSubcategoryUser, 8, http.StatusBadRequest, "password is wrong")
ErrUsernameAlreadyExists = NewNormalError(NormalSubcategoryUser, 9, http.StatusBadRequest, "username already exists")
ErrUserEmailAlreadyExists = NewNormalError(NormalSubcategoryUser, 10, http.StatusBadRequest, "email already exists")
ErrUserRegistrationNotAllowed = NewNormalError(NormalSubcategoryUser, 11, http.StatusBadRequest, "user registration not allowed")
)
+1
View File
@@ -302,6 +302,7 @@ export default {
'password is wrong': 'Password is wrong',
'username already exists': 'Username already exists',
'email already exists': 'Email already exists',
'user registration not allowed': 'User registration is not allowed',
'login name is invalid': 'Login name is invalid',
'login name or password is invalid': 'Login name or password is invalid',
'login name or password is wrong': 'Login name or password is wrong',
+1
View File
@@ -302,6 +302,7 @@ export default {
'password is wrong': '密码错误',
'username already exists': '用户名已经存在',
'email already exists': '邮箱已经存在',
'user registration not allowed': '不允许用户注册',
'login name is invalid': '登录名无效',
'login name or password is invalid': '登录名或密码无效',
'login name or password is wrong': '登录名或密码错误',