MCPcopy Index your code
hub / github.com/apache/answer / UserChangeEmailSendCode

Method UserChangeEmailSendCode

internal/service/content/user_service.go:656–706  ·  view source on GitHub ↗

UserChangeEmailSendCode user change email verification

(ctx context.Context, req *schema.UserChangeEmailSendCodeReq)

Source from the content-addressed store, hash-verified

654
655// UserChangeEmailSendCode user change email verification
656func (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
709func (us *UserService) UserChangeEmailVerify(ctx context.Context, content string) (resp *schema.UserLoginResp, err error) {

Callers

nothing calls this directly

Calls 11

verifyPasswordMethod · 0.95
getSiteUrlMethod · 0.95
ToJSONStringMethod · 0.95
TrFunction · 0.92
GetLangByCtxFunction · 0.92
GenerateTokenFunction · 0.92
RegisterTemplateMethod · 0.80
ChangeEmailTemplateMethod · 0.80
SendAndSaveCodeMethod · 0.80
GetByUserIDMethod · 0.65
GetByEmailMethod · 0.65

Tested by

no test coverage detected