19cc7a778c
- Deleted old gRPC service definitions in `game_grpc.pb.go` and `public.go`. - Added new API server implementations for objectstory, player, and shop services. - Introduced configuration files for new APIs in `etc/*.yaml`. - Created main entry points for each service in `objectstory.go`, `player.go`, and `shop.go`. - Removed unused user update handler and user API files. - Added utility functions for context management and HTTP header parsing. - Introduced PostgreSQL backup configuration in `backup/postgreSql.yaml`.
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
// Code scaffolded by goctl. Safe to edit.
|
|
// goctl 1.9.2
|
|
|
|
package auth
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"juwan-backend/app/users/rpc/pb"
|
|
"juwan-backend/app/users/rpc/usercenter"
|
|
"juwan-backend/common/utils/contextj"
|
|
"juwan-backend/common/utils/pwdUtils"
|
|
"regexp"
|
|
|
|
"juwan-backend/app/users/api/internal/svc"
|
|
"juwan-backend/app/users/api/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type RegisterLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 用户注册
|
|
func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
|
|
return &RegisterLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
var usernameRegex = regexp.MustCompile("^[a-zA-Z0-9_]+$")
|
|
|
|
func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterResp, err error) {
|
|
existingUser, err := l.svcCtx.UserRpc.GetUserByUsername(l.ctx, &pb.GetUserByUsernameReq{
|
|
Username: req.Username,
|
|
})
|
|
if len(req.Username) < 3 {
|
|
return nil, errors.New("username must be at least 3 characters long")
|
|
}
|
|
if len(req.Username) > 20 {
|
|
return nil, errors.New("username must be at most 20 characters long")
|
|
}
|
|
if !usernameRegex.MatchString(req.Username) {
|
|
return nil, errors.New("username can only contain letters, numbers, and underscores")
|
|
}
|
|
if err == nil && existingUser != nil {
|
|
return nil, errors.New("user already exists")
|
|
}
|
|
|
|
hashedPassword, err := pwdUtils.HashPassword(req.Password)
|
|
if err != nil {
|
|
return nil, errors.New("hash password failed")
|
|
}
|
|
|
|
requestId, err := contextj.RequestIdFrom(l.ctx)
|
|
if err != nil {
|
|
logx.Errorf("contextj.RequestIdFrom failed: %v", err)
|
|
return nil, errors.New("contextj.RequestIdFrom failed")
|
|
}
|
|
|
|
res, err := l.svcCtx.UserRpc.Register(l.ctx, &usercenter.RegisterReq{
|
|
Username: req.Username,
|
|
Passwd: hashedPassword,
|
|
Phone: req.Username,
|
|
Vcode: req.Vcode,
|
|
Email: req.Email,
|
|
RequestId: requestId,
|
|
})
|
|
if err != nil {
|
|
logx.Error("failed to register user: ", err)
|
|
return nil, errors.New("failed to register user")
|
|
}
|
|
|
|
// 返回响应
|
|
return &types.RegisterResp{
|
|
AccessToken: "",
|
|
RefreshToken: res.Res,
|
|
User: types.User{},
|
|
}, nil
|
|
}
|