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

Email log in and register #271

Merged
merged 10 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
21 changes: 15 additions & 6 deletions internal/rpc/chat/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,10 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
if req.User == nil {
return nil, errs.ErrArgs.Wrap("user is nil")
}
if (req.User.AreaCode == "" && req.User.PhoneNumber != "") || (req.User.AreaCode != "" && req.User.PhoneNumber == "") {
return nil, errs.ErrArgs.Wrap("area code or phone number error")
}
if req.User.PhoneNumber == "" && req.User.Account == "" {
return nil, errs.ErrArgs.Wrap("phone number and account is empty")
if req.User.Email == "" {
if (req.User.AreaCode == "" && req.User.PhoneNumber != "") || (req.User.AreaCode != "" && req.User.PhoneNumber == "") {
return nil, errs.ErrArgs.Wrap("area code or phone number error, no email provide")
}
}
var usedInvitationCode bool
if !isAdmin {
Expand Down Expand Up @@ -329,6 +328,14 @@ 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)
if err == nil {
return nil, eerrs.ErrEmailAlreadyRegister.Wrap()
} else if !o.Database.IsNotFound(err) {
return nil, err
}
}
register := &chat2.Register{
UserID: req.User.UserID,
DeviceID: req.DeviceID,
Expand Down Expand Up @@ -397,8 +404,10 @@ func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginRes
return nil, errs.ErrArgs.Wrap("area code must start with +")
}
attribute, err = o.Database.GetAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
} else if req.Email != "" {
attribute, err = o.Database.GetAttributeByEmail(ctx, req.Email)
} else {
err = errs.ErrArgs.Wrap("account or phone number must be set")
err = errs.ErrArgs.Wrap("account or phone number or email must be set")
}
if err != nil {
if o.Database.IsNotFound(err) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/common/db/database/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type ChatDatabaseInterface interface {
GetAttribute(ctx context.Context, userID string) (*table.Attribute, error)
GetAttributeByAccount(ctx context.Context, account string) (*table.Attribute, error)
GetAttributeByPhone(ctx context.Context, areaCode string, phoneNumber string) (*table.Attribute, error)
GetAttributeByEmail(ctx context.Context, email string) (*table.Attribute, error)
LoginRecord(ctx context.Context, record *table.UserLoginRecord, verifyCodeID *uint) error
UpdatePassword(ctx context.Context, userID string, password string) error
UpdatePasswordAndDeleteVerifyCode(ctx context.Context, userID string, password string, code uint) error
Expand Down Expand Up @@ -222,7 +223,9 @@ func (o *ChatDatabase) GetAttributeByAccount(ctx context.Context, account string
func (o *ChatDatabase) GetAttributeByPhone(ctx context.Context, areaCode string, phoneNumber string) (*table.Attribute, error) {
return o.attribute.TakePhone(ctx, areaCode, phoneNumber)
}

func (o *ChatDatabase) GetAttributeByEmail(ctx context.Context, email string) (*table.Attribute, error) {
return o.attribute.TakeEmail(ctx, email)
}
func (o *ChatDatabase) LoginRecord(ctx context.Context, record *table.UserLoginRecord, verifyCodeID *uint) error {
return o.tx.Transaction(func(tx any) error {
if err := o.userLoginRecord.NewTx(tx).Create(ctx, record); err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/eerrs/predefine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ var (
ErrInvitationNotFound = errs.NewCodeError(20011, "InvitationNotFound") // 邀请码不存在
ErrForbidden = errs.NewCodeError(20012, "Forbidden") // 限制登录注册
ErrRefuseFriend = errs.NewCodeError(20013, "RefuseFriend") // 拒绝添加好友
ErrEmailAlreadyRegister = errs.NewCodeError(20014, "EmailAlreadyRegister") // 邮箱已经注册
)
112 changes: 69 additions & 43 deletions pkg/proto/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,42 @@ func (x *SendVerifyCodeReq) Check() error {
if x.UsedFor < constant.VerificationCodeForRegister || x.UsedFor > constant.VerificationCodeForLogin {
return errs.ErrArgs.Wrap("usedFor flied is empty")
}
if x.AreaCode == "" {
return errs.ErrArgs.Wrap("AreaCode is empty")
} else if err := AreaCodeCheck(x.AreaCode); err != nil {
return err
}
if x.PhoneNumber == "" {
return errs.ErrArgs.Wrap("PhoneNumber is empty")
} else if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
return err
if x.Email == "" {
if x.AreaCode == "" {
return errs.ErrArgs.Wrap("AreaCode is empty")
} else if err := AreaCodeCheck(x.AreaCode); err != nil {
return err
}
if x.PhoneNumber == "" {
return errs.ErrArgs.Wrap("PhoneNumber is empty")
} else if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
return err
}
} else {
if err := EmailCheck(x.Email); err != nil {
return err
}
}

return nil
}

func (x *VerifyCodeReq) Check() error {
if x.AreaCode == "" {
return errs.ErrArgs.Wrap("AreaCode is empty")
} else if err := AreaCodeCheck(x.AreaCode); err != nil {
return err
}
if x.PhoneNumber == "" {
return errs.ErrArgs.Wrap("PhoneNumber is empty")
} else if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
return err
if x.Email == "" {
if x.AreaCode == "" {
return errs.ErrArgs.Wrap("AreaCode is empty")
} else if err := AreaCodeCheck(x.AreaCode); err != nil {
return err
}
if x.PhoneNumber == "" {
return errs.ErrArgs.Wrap("PhoneNumber is empty")
} else if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
return err
}
} else {
if err := EmailCheck(x.Email); err != nil {
return err
}
}
if x.VerifyCode == "" {
return errs.ErrArgs.Wrap("VerifyCode is empty")
Expand All @@ -108,17 +121,18 @@ func (x *RegisterUserReq) Check() error {
if x.User == nil {
return errs.ErrArgs.Wrap("user is empty")
}
if x.User.AreaCode == "" {
return errs.ErrArgs.Wrap("AreaCode is empty")
} else if err := AreaCodeCheck(x.User.AreaCode); err != nil {
return err
}
if x.User.PhoneNumber == "" {
return errs.ErrArgs.Wrap("PhoneNumber is empty")
} else if err := PhoneNumberCheck(x.User.PhoneNumber); err != nil {
return err
}
if x.User.Email != "" {
if x.User.Email == "" {
if x.User.AreaCode == "" {
return errs.ErrArgs.Wrap("AreaCode is empty")
} else if err := AreaCodeCheck(x.User.AreaCode); err != nil {
return err
}
if x.User.PhoneNumber == "" {
return errs.ErrArgs.Wrap("PhoneNumber is empty")
} else if err := PhoneNumberCheck(x.User.PhoneNumber); err != nil {
return err
}
} else {
if err := EmailCheck(x.User.Email); err != nil {
return err
}
Expand All @@ -130,13 +144,19 @@ func (x *LoginReq) Check() error {
if x.Platform < constant2.IOSPlatformID || x.Platform > constant2.AdminPlatformID {
return errs.ErrArgs.Wrap("platform is invalid")
}
if x.PhoneNumber != "" {
if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
if x.Email == "" {
if x.AreaCode == "" {
return errs.ErrArgs.Wrap("AreaCode is empty")
} else if err := AreaCodeCheck(x.AreaCode); err != nil {
return err
}
}
if x.AreaCode != "" {
if err := AreaCodeCheck(x.AreaCode); err != nil {
if x.PhoneNumber == "" {
return errs.ErrArgs.Wrap("PhoneNumber is empty")
} else if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
return err
}
} else {
if err := EmailCheck(x.Email); err != nil {
return err
}
}
Expand All @@ -147,15 +167,21 @@ func (x *ResetPasswordReq) Check() error {
if x.Password == "" {
return errs.ErrArgs.Wrap("password is empty")
}
if x.AreaCode == "" {
return errs.ErrArgs.Wrap("AreaCode is empty")
} else if err := AreaCodeCheck(x.AreaCode); err != nil {
return err
}
if x.PhoneNumber == "" {
return errs.ErrArgs.Wrap("PhoneNumber is empty")
} else if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
return err
if x.Email == "" {
if x.AreaCode == "" {
return errs.ErrArgs.Wrap("AreaCode is empty")
} else if err := AreaCodeCheck(x.AreaCode); err != nil {
return err
}
if x.PhoneNumber == "" {
return errs.ErrArgs.Wrap("PhoneNumber is empty")
} else if err := PhoneNumberCheck(x.PhoneNumber); err != nil {
return err
}
} else {
if err := EmailCheck(x.Email); err != nil {
return err
}
}
if x.VerifyCode == "" {
return errs.ErrArgs.Wrap("VerifyCode is empty")
Expand Down
Loading
Loading