UserChangeEmailSendCode user change email verification
(ctx context.Context, req *schema.UserChangeEmailSendCodeReq)
| 654 | |
| 655 | // UserChangeEmailSendCode user change email verification |
| 656 | func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.UserChangeEmailSendCodeReq) ( |
| 657 | resp []*validator.FormErrorField, err error) { |
| 658 | userInfo, exist, err := us.userRepo.GetByUserID(ctx, req.UserID) |
| 659 | if err != nil { |
| 660 | return nil, err |
| 661 | } |
| 662 | if !exist { |
| 663 | return nil, errors.BadRequest(reason.UserNotFound) |
| 664 | } |
| 665 | |
| 666 | // If user's email already verified, then must verify password first. |
| 667 | if userInfo.MailStatus == entity.EmailStatusAvailable && !us.verifyPassword(ctx, req.Pass, userInfo.Pass) { |
| 668 | resp = append(resp, &validator.FormErrorField{ |
| 669 | ErrorField: "pass", |
| 670 | ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.OldPasswordVerificationFailed), |
| 671 | }) |
| 672 | return resp, errors.BadRequest(reason.OldPasswordVerificationFailed) |
| 673 | } |
| 674 | |
| 675 | _, exist, err = us.userRepo.GetByEmail(ctx, req.Email) |
| 676 | if err != nil { |
| 677 | return nil, err |
| 678 | } |
| 679 | if exist { |
| 680 | resp = append([]*validator.FormErrorField{}, &validator.FormErrorField{ |
| 681 | ErrorField: "e_mail", |
| 682 | ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.EmailDuplicate), |
| 683 | }) |
| 684 | return resp, errors.BadRequest(reason.EmailDuplicate) |
| 685 | } |
| 686 | |
| 687 | data := &schema.EmailCodeContent{ |
| 688 | Email: req.Email, |
| 689 | UserID: req.UserID, |
| 690 | } |
| 691 | code := token.GenerateToken() |
| 692 | var title, body string |
| 693 | verifyEmailURL := fmt.Sprintf("%s/users/confirm-new-email?code=%s", us.getSiteUrl(ctx), code) |
| 694 | if userInfo.MailStatus == entity.EmailStatusToBeVerified { |
| 695 | title, body, err = us.emailService.RegisterTemplate(ctx, verifyEmailURL) |
| 696 | } else { |
| 697 | title, body, err = us.emailService.ChangeEmailTemplate(ctx, verifyEmailURL) |
| 698 | } |
| 699 | if err != nil { |
| 700 | return nil, err |
| 701 | } |
| 702 | log.Infof("send email confirmation %s", verifyEmailURL) |
| 703 | |
| 704 | go us.emailService.SendAndSaveCode(ctx, userInfo.ID, req.Email, title, body, code, data.ToJSONString()) |
| 705 | return nil, nil |
| 706 | } |
| 707 | |
| 708 | // UserChangeEmailVerify user change email verify code |
| 709 | func (us *UserService) UserChangeEmailVerify(ctx context.Context, content string) (resp *schema.UserLoginResp, err error) { |
nothing calls this directly
no test coverage detected