Skip to content

Commit

Permalink
Fix email related functions (#301)
Browse files Browse the repository at this point in the history
* fix bug

* Your commit message

* fix bug register email

* fix bug email resetpassword

* fix bug mysql sleep

* fix bug login.go

* fix bug login.go

* fix bug login.go

---------

Co-authored-by: Gordon <[email protected]>
  • Loading branch information
AndrewZuo01 and FGadvancer authored Dec 18, 2023
1 parent 5823c24 commit b0db9d1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scripts-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
-v "/etc/localtime:/etc/localtime" \
-e MYSQL_ROOT_PASSWORD="openIM123" \
--restart always \
mysql:5.7
mysql:5.7
sleep 30;
- name: start all services
run: |
Expand Down
40 changes: 32 additions & 8 deletions internal/rpc/chat/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,22 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe
}
}
case constant.VerificationCodeForLogin, constant.VerificationCodeForResetPassword:
_, err := o.Database.TakeAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
if o.Database.IsNotFound(err) {
return nil, errs.ErrArgs.Wrap("phone unregistered")
} else if err != nil {
return nil, err
if req.Email == "" {
_, err := o.Database.TakeAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
if o.Database.IsNotFound(err) {
return nil, eerrs.ErrAccountNotFound.Wrap("phone unregistered")
} else if err != nil {
return nil, err
}
} else {
_, err := o.Database.TakeAttributeByEmail(ctx, req.Email)
if o.Database.IsNotFound(err) {
return nil, eerrs.ErrAccountNotFound.Wrap("email unregistered")
} else if err != nil {
return nil, err
}
}

default:
return nil, errs.ErrArgs.Wrap("used unknown")
}
Expand Down Expand Up @@ -275,9 +285,16 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
return nil, err
}
}
if _, err := o.verifyCode(ctx, o.verifyCodeJoin(req.User.AreaCode, req.User.PhoneNumber), req.VerifyCode); err != nil {
return nil, err
if req.User.Email == "" {
if _, err := o.verifyCode(ctx, o.verifyCodeJoin(req.User.AreaCode, req.User.PhoneNumber), req.VerifyCode); err != nil {
return nil, err
}
} else {
if _, err := o.verifyCode(ctx, req.User.Email, req.VerifyCode); err != nil {
return nil, err
}
}

}
log.ZDebug(ctx, "usedInvitationCode", usedInvitationCode)
if req.User.UserID == "" {
Expand Down Expand Up @@ -427,7 +444,14 @@ func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginRes
}
var verifyCodeID *uint
if req.Password == "" {
id, err := o.verifyCode(ctx, o.verifyCodeJoin(req.AreaCode, req.PhoneNumber), req.VerifyCode)
var id uint
var err error
if req.Email == "" {
id, err = o.verifyCode(ctx, o.verifyCodeJoin(req.AreaCode, req.PhoneNumber), req.VerifyCode)
} else {
id, err = o.verifyCode(ctx, req.Email, req.VerifyCode)
}

if err != nil {
return nil, err
}
Expand Down
10 changes: 8 additions & 2 deletions internal/rpc/chat/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package chat

import (
"context"

"github.com/OpenIMSDK/tools/log"

"github.com/OpenIMSDK/tools/errs"
Expand All @@ -31,7 +30,14 @@ func (o *chatSvr) ResetPassword(ctx context.Context, req *chat.ResetPasswordReq)
if req.Password == "" {
return nil, errs.ErrArgs.Wrap("password must be set")
}
verifyCodeID, err := o.verifyCode(ctx, o.verifyCodeJoin(req.AreaCode, req.PhoneNumber), req.VerifyCode)
var verifyCodeID uint
var err error
if req.Email == "" {
verifyCodeID, err = o.verifyCode(ctx, o.verifyCodeJoin(req.AreaCode, req.PhoneNumber), req.VerifyCode)
} else {
verifyCodeID, err = o.verifyCode(ctx, req.Email, req.VerifyCode)
}

if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/proto/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (x *SearchLogsReq) Check() error {
}

func EmailCheck(email string) error {
pattern := `^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z]\.){1,4}[a-z]{2,4}$`
pattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
if err := regexMatch(pattern, email); err != nil {
return errs.Wrap(err, "Email is invalid")
}
Expand Down

0 comments on commit b0db9d1

Please sign in to comment.