Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: user change email if email exist return form error

LinkinStar55060c1a10a0
diff --git a/internal/controller/user_controller.go b/internal/controller/user_controller.go
index 3d4d2d3a..92d491fe 100644
--- a/internal/controller/user_controller.go
+++ b/internal/controller/user_controller.go
@@ -495,6 +495,10 @@ func (uc *UserController) UserChangeEmailSendCode(ctx *gin.Context) {
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
// If the user is not logged in, the api cannot be used.
// If the user email is not verified, that also can use this api to modify the email.
+ if len(req.UserID) == 0 {
+ handler.HandleResponse(ctx, errors.Unauthorized(reason.UnauthorizedError), nil)
+ return
+ }
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
if !captchaPass {
@@ -506,13 +510,15 @@ func (uc *UserController) UserChangeEmailSendCode(ctx *gin.Context) {
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
return
}
-
- if len(req.UserID) == 0 {
- handler.HandleResponse(ctx, errors.Unauthorized(reason.UnauthorizedError), nil)
+ _, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
+ resp, err := uc.userService.UserChangeEmailSendCode(ctx, req)
+ if err != nil {
+ if resp != nil {
+ resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
+ }
+ handler.HandleResponse(ctx, err, resp)
return
}
- _, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
- err := uc.userService.UserChangeEmailSendCode(ctx, req)
handler.HandleResponse(ctx, err, nil)
}
diff --git a/internal/service/user_service.go b/internal/service/user_service.go
index a4fc1fad..8673c3d2 100644
--- a/internal/service/user_service.go
+++ b/internal/service/user_service.go
@@ -477,21 +477,26 @@ func (us *UserService) encryptPassword(ctx context.Context, Pass string) (string
}
// UserChangeEmailSendCode user change email verification
-func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.UserChangeEmailSendCodeReq) error {
+func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.UserChangeEmailSendCodeReq) (
+ resp *schema.UserVerifyEmailErrorResponse, err error) {
userInfo, exist, err := us.userRepo.GetByUserID(ctx, req.UserID)
if err != nil {
- return err
+ return nil, err
}
if !exist {
- return errors.BadRequest(reason.UserNotFound)
+ return nil, errors.BadRequest(reason.UserNotFound)
}
_, exist, err = us.userRepo.GetByEmail(ctx, req.Email)
if err != nil {
- return err
+ return nil, err
}
if exist {
- return errors.BadRequest(reason.EmailDuplicate)
+ resp = &schema.UserVerifyEmailErrorResponse{
+ Key: "e_mail",
+ Value: reason.EmailDuplicate,
+ }
+ return resp, errors.BadRequest(reason.EmailDuplicate)
}
data := &schema.EmailCodeContent{
@@ -507,12 +512,12 @@ func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.
title, body, err = us.emailService.ChangeEmailTemplate(ctx, verifyEmailURL)
}
if err != nil {
- return err
+ return nil, err
}
log.Infof("send email confirmation %s", verifyEmailURL)
go us.emailService.Send(context.Background(), req.Email, title, body, code, data.ToJSONString())
- return nil
+ return nil, nil
}
// UserChangeEmailVerify user change email verify code