Skip to content

Commit

Permalink
Email log in and register (#271)
Browse files Browse the repository at this point in the history
* add email log in

* update

* update

* proto compile

* FIX BUG

* FIX BUG

* FIX BUG

* FIX BUG

* FIX BUG
  • Loading branch information
AndrewZuo01 authored Dec 1, 2023
1 parent 559192d commit 95561a0
Show file tree
Hide file tree
Showing 7 changed files with 593 additions and 523 deletions.
26 changes: 18 additions & 8 deletions internal/rpc/chat/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe
}
_, err := o.Database.TakeAttributeByEmail(ctx, req.Email)
if err == nil {
return nil, eerrs.ErrPhoneAlreadyRegister.Wrap("email already register")
return nil, eerrs.ErrEmailAlreadyRegister.Wrap("email already register")
} else if !o.Database.IsNotFound(err) {
return nil, err
}
Expand Down Expand Up @@ -239,7 +239,7 @@ func (o *chatSvr) genVerifyCode() string {

func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (*chat.RegisterUserResp, error) {
resp := &chat.RegisterUserResp{}
defer log.ZDebug(ctx, "return")

isAdmin, err := o.Admin.CheckNilOrAdmin(ctx)
ctx = mctx.WithAdminUser(ctx)
if err != nil {
Expand All @@ -248,11 +248,11 @@ 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")
log.ZDebug(ctx, "email", req.User.Email)
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 +329,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 @@ -403,8 +411,10 @@ func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginRes
return nil, errs.ErrArgs.Wrap("area code must be number")
}
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
18 changes: 14 additions & 4 deletions internal/rpc/chat/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,21 @@ func (o *chatSvr) ResetPassword(ctx context.Context, req *chat.ResetPasswordReq)
if err != nil {
return nil, err
}
attribute, err := o.Database.GetAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
if err != nil {
return nil, err

if req.Email == "" {
attribute, err := o.Database.GetAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
if err != nil {
return nil, err
}
err = o.Database.UpdatePasswordAndDeleteVerifyCode(ctx, attribute.UserID, req.Password, verifyCodeID)
} else {
attribute, err := o.Database.GetAttributeByEmail(ctx, req.Email)
if err != nil {
return nil, err
}
err = o.Database.UpdatePasswordAndDeleteVerifyCode(ctx, attribute.UserID, req.Password, verifyCodeID)
}
err = o.Database.UpdatePasswordAndDeleteVerifyCode(ctx, attribute.UserID, req.Password, verifyCodeID)

if err != nil {
return nil, 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

0 comments on commit 95561a0

Please sign in to comment.