diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml index 8e8f0a29..2d08b02a 100644 --- a/i18n/en_US.yaml +++ b/i18n/en_US.yaml @@ -50,6 +50,14 @@ error: other: "You can't vote for your own post!" not_found: other: "object not found" + verification_failed: + other: "verification failed" + email_or_password_incorrect: + other: "email or password incorrect" + old_password_verification_failed: + other: "the old password verification failed" + new_password_same_as_previous_setting: + other: "The new password is the same as the previous setting" question: not_found: other: "question not found" diff --git a/internal/controller/user_controller.go b/internal/controller/user_controller.go index b7e8cb4b..82d6bebf 100644 --- a/internal/controller/user_controller.go +++ b/internal/controller/user_controller.go @@ -9,6 +9,7 @@ import ( "github.com/segmentfault/answer/internal/base/handler" "github.com/segmentfault/answer/internal/base/middleware" "github.com/segmentfault/answer/internal/base/reason" + "github.com/segmentfault/answer/internal/base/translator" "github.com/segmentfault/answer/internal/schema" "github.com/segmentfault/answer/internal/service" "github.com/segmentfault/answer/internal/service/action" @@ -113,20 +114,24 @@ func (uc *UserController) UserEmailLogin(ctx *gin.Context) { captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecord_Type_Login, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode) if !captchaPass { - handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), gin.H{ - "key": "captcha_code", - "value": "verification failed", - }) + resp := schema.UserVerifyEmailErrorResponse{ + Key: "captcha_code", + Value: "error.object.verification_failed", + } + resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value) + handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp) return } resp, err := uc.userService.EmailLogin(ctx, req) if err != nil { _, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecord_Type_Login, ctx.ClientIP()) - handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), gin.H{ - "key": "e_mail", - "value": "Email or password incorrect", - }) + resp := schema.UserVerifyEmailErrorResponse{ + Key: "e_mail", + Value: "error.object.email_or_password_incorrect", + } + resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value) + handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp) return } uc.actionService.ActionRecordDel(ctx, schema.ActionRecord_Type_Login, ctx.ClientIP()) @@ -149,10 +154,12 @@ func (uc *UserController) RetrievePassWord(ctx *gin.Context) { } captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecord_Type_Find_Pass, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode) if !captchaPass { - handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), gin.H{ - "key": "captcha_code", - "value": "verification failed", - }) + resp := schema.UserVerifyEmailErrorResponse{ + Key: "captcha_code", + Value: "error.object.verification_failed", + } + resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value) + handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp) return } _, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecord_Type_Find_Pass, ctx.ClientIP()) @@ -278,10 +285,13 @@ func (uc *UserController) UserVerifyEmailSend(ctx *gin.Context) { captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecord_Type_Email, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode) if !captchaPass { - handler.HandleResponse(ctx, errors.BadRequest(reason.RequestFormatError), gin.H{ - "key": "captcha_code", - "value": "verification failed", - }) + resp := schema.UserVerifyEmailErrorResponse{ + Key: "captcha_code", + Value: "error.object.verification_failed", + } + resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value) + handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp) + return } uc.actionService.ActionRecordAdd(ctx, schema.ActionRecord_Type_Email, ctx.ClientIP()) @@ -312,17 +322,22 @@ func (uc *UserController) UserModifyPassWord(ctx *gin.Context) { return } if !oldPassVerification { - handler.HandleResponse(ctx, errors.BadRequest(reason.RequestFormatError), gin.H{ - "key": "old_pass", - "value": "the old password verification failed", - }) + resp := schema.UserVerifyEmailErrorResponse{ + Key: "captcha_code", + Value: "error.object.old_password_verification_failed", + } + resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value) + handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp) return } if req.OldPass == req.Pass { - handler.HandleResponse(ctx, errors.BadRequest(reason.RequestFormatError), gin.H{ - "key": "pass", - "value": "The new password is the same as the previous setting", - }) + + resp := schema.UserVerifyEmailErrorResponse{ + Key: "captcha_code", + Value: "error.object.new_password_same_as_previous_setting", + } + resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value) + handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp) return } err = uc.userService.UserModifyPassWord(ctx, req) diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.go index 98027870..4ac19e7f 100644 --- a/internal/schema/user_schema.go +++ b/internal/schema/user_schema.go @@ -352,3 +352,8 @@ type UserVerifyEmailSendReq struct { CaptchaID string `validate:"omitempty,gt=0,lte=500" json:"captcha_id"` CaptchaCode string `validate:"omitempty,gt=0,lte=500" json:"captcha_code"` } + +type UserVerifyEmailErrorResponse struct { + Key string `json:"key"` + Value string `json:"value"` +}