Skip to content

Commit

Permalink
fix: default verifyCode type and admin register user (#274)
Browse files Browse the repository at this point in the history
* fix: default verifyCode type and admin register user

* fix: default verifyCode type and admin register user

* fix: default verifyCode type and admin register user

* fix: default verifyCode type and admin register user
  • Loading branch information
withchao committed Nov 21, 2023
1 parent bb1d6cb commit 559192d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ verifyCode:
maxCount: 10 # 单位时间内最大获取次数
superCode: "666666" # 超级验证码(只有use为空时使用)
len: 6 # 验证码长度
use: "ali" # 使用的验证码服务(use: "ali")
use: "" # 使用的验证码服务(use: "ali")
ali:
endpoint: "dysmsapi.aliyuncs.com"
accessKeyId: ""
Expand Down
11 changes: 11 additions & 0 deletions internal/api/mw.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,14 @@ func (o *MW) CheckUser(c *gin.Context) {
}
o.setToken(c, userID, constant.NormalUser)
}

func (o *MW) CheckAdminOrNil(c *gin.Context) {
defer c.Next()
userID, userType, _, err := o.parseToken(c)
if err != nil {
return
}
if userType == constant.AdminUser {
o.setToken(c, userID, constant.AdminUser)
}
}
2 changes: 1 addition & 1 deletion internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewChatRoute(router gin.IRouter, discov discoveryregistry.SvcDiscoveryRegis
account := router.Group("/account")
account.POST("/code/send", chat.SendVerifyCode) // 发送验证码
account.POST("/code/verify", chat.VerifyCode) // 校验验证码
account.POST("/register", chat.RegisterUser) // 注册
account.POST("/register", mw.CheckAdminOrNil, chat.RegisterUser) // 注册
account.POST("/login", chat.Login) // 登录
account.POST("/password/reset", chat.ResetPassword) // 忘记密码
account.POST("/password/change", mw.CheckToken, chat.ChangePassword) // 修改密码
Expand Down
14 changes: 10 additions & 4 deletions internal/rpc/chat/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe
return nil, errs.ErrArgs.Wrap("area code or phone number is empty")
}
if req.AreaCode[0] != '+' {
return nil, errs.ErrArgs.Wrap("area code must start with +")
req.AreaCode = "+" + req.AreaCode
}
if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil {
return nil, errs.ErrArgs.Wrap("area code must be number")
Expand Down Expand Up @@ -306,7 +306,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
}
if req.User.PhoneNumber != "" {
if req.User.AreaCode[0] != '+' {
return nil, errs.ErrArgs.Wrap("area code must start with +")
req.User.AreaCode = "+" + req.User.AreaCode
}
if _, err := strconv.ParseUint(req.User.AreaCode[1:], 10, 64); err != nil {
return nil, errs.ErrArgs.Wrap("area code must be number")
Expand Down Expand Up @@ -393,8 +393,14 @@ func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginRes
if req.Account != "" {
attribute, err = o.Database.GetAttributeByAccount(ctx, req.Account)
} else if req.PhoneNumber != "" {
if req.AreaCode == "" || req.AreaCode[0] != '+' {
return nil, errs.ErrArgs.Wrap("area code must start with +")
if req.AreaCode == "" {
return nil, errs.ErrArgs.Wrap("area code must")
}
if req.AreaCode[0] != '+' {
req.AreaCode = "+" + req.AreaCode
}
if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil {
return nil, errs.ErrArgs.Wrap("area code must be number")
}
attribute, err = o.Database.GetAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
} else {
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/mw/gin_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GinLog() gin.HandlerFunc {
return
}
start := time.Now()
log.ZDebug(c, "gin request", "method", c.Request.Method, "uri", c.Request.RequestURI, "req header", c.Request.Header, "req body", string(req))
log.ZDebug(c, "gin request", "method", c.Request.Method, "uri", c.Request.RequestURI, "req", string(req))
c.Request.Body = io.NopCloser(bytes.NewReader(req))
writer := &responseWriter{
ResponseWriter: c.Writer,
Expand All @@ -52,6 +52,6 @@ func GinLog() gin.HandlerFunc {
c.Writer = writer
c.Next()
resp := writer.buf.Bytes()
log.ZDebug(c, "gin response", "time", time.Since(start), "status", c.Writer.Status(), "resp header", c.Writer.Header(), "resp", string(resp))
log.ZDebug(c, "gin response", "time", time.Since(start), "status", c.Writer.Status(), "resp", string(resp))
}
}
8 changes: 4 additions & 4 deletions pkg/proto/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ func EmailCheck(email string) error {
}

func AreaCodeCheck(areaCode string) error {
pattern := `\+[1-9][0-9]{1,2}`
if err := regexMatch(pattern, areaCode); err != nil {
return errs.Wrap(err, "AreaCode is invalid")
}
//pattern := `\+[1-9][0-9]{1,2}`
//if err := regexMatch(pattern, areaCode); err != nil {
// return errs.Wrap(err, "AreaCode is invalid")
//}
return nil
}

Expand Down

0 comments on commit 559192d

Please sign in to comment.