Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix email related functions #301

Merged
merged 9 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading