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

Add emailRegister check #308

Merged
merged 15 commits into from
Dec 18, 2023
46 changes: 38 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 All @@ -304,6 +321,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
return nil, err
}
}
var registerType int32
if req.User.PhoneNumber != "" {
if req.User.AreaCode[0] != '+' {
req.User.AreaCode = "+" + req.User.AreaCode
Expand All @@ -320,7 +338,9 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
} else if !o.Database.IsNotFound(err) {
return nil, err
}
registerType = constant.PhoneRegister
}

if req.User.Account != "" {
_, err := o.Database.TakeAttributeByAccount(ctx, req.User.Account)
if err == nil {
Expand All @@ -329,8 +349,10 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
return nil, err
}
}

if req.User.Email != "" {
_, err := o.Database.TakeAttributeByEmail(ctx, req.User.Email)
registerType = constant.EmailRegister
if err == nil {
return nil, eerrs.ErrEmailAlreadyRegister.Wrap()
} else if !o.Database.IsNotFound(err) {
Expand Down Expand Up @@ -368,6 +390,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
AllowVibration: constant.DefaultAllowVibration,
AllowBeep: constant.DefaultAllowBeep,
AllowAddFriend: constant.DefaultAllowAddFriend,
RegisterType: registerType,
}
if err := o.Database.RegisterUser(ctx, register, account, attribute); err != nil {
return nil, err
Expand Down Expand Up @@ -427,7 +450,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
1 change: 1 addition & 0 deletions internal/rpc/chat/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func DbToPbUserFullInfo(attribute *chat.Attribute) *common.UserFullInfo {
AllowBeep: attribute.AllowBeep,
AllowVibration: attribute.AllowVibration,
GlobalRecvMsgOpt: attribute.GlobalRecvMsgOpt,
RegisterType: attribute.RegisterType,
}
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/common/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,8 @@ const (
const DefaultPlatform = 1

const CtxApiToken = "api-token"

const (
EmailRegister = 1
PhoneRegister = 2
)
1 change: 1 addition & 0 deletions pkg/common/db/table/chat/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Attribute struct {
AllowBeep int32 `gorm:"column:allow_beep;default:1"`
AllowAddFriend int32 `gorm:"column:allow_add_friend;default:1"`
GlobalRecvMsgOpt int32 `gorm:"column:global_recv_msg_opt;default:0"`
RegisterType int32 `gorm:"column:register_type"`
}

func (Attribute) TableName() string {
Expand Down
48 changes: 24 additions & 24 deletions pkg/proto/admin/admin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Loading