add user error return in i18n

This commit is contained in:
aichy126 2022-10-24 11:21:37 +08:00
parent ec950da888
commit a00fb70ff2
3 changed files with 52 additions and 24 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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"`
}