feat: user change email if email exist return form error

This commit is contained in:
LinkinStar 2022-11-09 14:59:07 +08:00
parent e81d9cfedd
commit 55060c1a10
2 changed files with 23 additions and 12 deletions

View File

@ -495,6 +495,10 @@ func (uc *UserController) UserChangeEmailSendCode(ctx *gin.Context) {
req.UserID = middleware.GetLoginUserIDFromContext(ctx) req.UserID = middleware.GetLoginUserIDFromContext(ctx)
// If the user is not logged in, the api cannot be used. // 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 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) captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
if !captchaPass { if !captchaPass {
@ -506,13 +510,15 @@ func (uc *UserController) UserChangeEmailSendCode(ctx *gin.Context) {
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp) handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
return return
} }
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
if len(req.UserID) == 0 { resp, err := uc.userService.UserChangeEmailSendCode(ctx, req)
handler.HandleResponse(ctx, errors.Unauthorized(reason.UnauthorizedError), nil) if err != nil {
if resp != nil {
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
}
handler.HandleResponse(ctx, err, resp)
return return
} }
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
err := uc.userService.UserChangeEmailSendCode(ctx, req)
handler.HandleResponse(ctx, err, nil) handler.HandleResponse(ctx, err, nil)
} }

View File

@ -477,21 +477,26 @@ func (us *UserService) encryptPassword(ctx context.Context, Pass string) (string
} }
// UserChangeEmailSendCode user change email verification // 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) userInfo, exist, err := us.userRepo.GetByUserID(ctx, req.UserID)
if err != nil { if err != nil {
return err return nil, err
} }
if !exist { if !exist {
return errors.BadRequest(reason.UserNotFound) return nil, errors.BadRequest(reason.UserNotFound)
} }
_, exist, err = us.userRepo.GetByEmail(ctx, req.Email) _, exist, err = us.userRepo.GetByEmail(ctx, req.Email)
if err != nil { if err != nil {
return err return nil, err
} }
if exist { 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{ data := &schema.EmailCodeContent{
@ -507,12 +512,12 @@ func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.
title, body, err = us.emailService.ChangeEmailTemplate(ctx, verifyEmailURL) title, body, err = us.emailService.ChangeEmailTemplate(ctx, verifyEmailURL)
} }
if err != nil { if err != nil {
return err return nil, err
} }
log.Infof("send email confirmation %s", verifyEmailURL) log.Infof("send email confirmation %s", verifyEmailURL)
go us.emailService.Send(context.Background(), req.Email, title, body, code, data.ToJSONString()) go us.emailService.Send(context.Background(), req.Email, title, body, code, data.ToJSONString())
return nil return nil, nil
} }
// UserChangeEmailVerify user change email verify code // UserChangeEmailVerify user change email verify code