add: user auth accomplished

This commit is contained in:
wwweww
2026-02-26 02:17:07 +08:00
parent 300058ad01
commit 60b6f40f9f
54 changed files with 1601 additions and 2303 deletions
@@ -5,6 +5,9 @@ package user
import (
"context"
"errors"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/converter"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
@@ -27,8 +30,21 @@ func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUs
}
}
func (l *GetUserInfoLogic) GetUserInfo(req *types.GetUserInfoReq) (resp *types.UserInfo, err error) {
func (l *GetUserInfoLogic) GetUserInfo(req *types.GetUserInfoReq) (resp types.UserInfo, err error) {
// todo: add your logic here and delete this line
pbUser, err := l.svcCtx.UserRpc.GetUsersById(l.ctx, &usercenter.GetUsersByIdReq{
Id: req.UserId,
})
if err != nil {
return types.UserInfo{}, errors.New("failed to get user info by userid")
}
user := types.UserInfo{}
err = converter.StructToStruct(&pbUser.Users, &user)
if err != nil {
logx.Errorf("struct to user info failed, err:%v.", err)
return types.UserInfo{}, errors.New("failed to get user info by userid")
}
return
//req.UserId
return user, nil
}
@@ -5,9 +5,11 @@ package user
import (
"context"
"errors"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"juwan-backend/app/users/rpc/usercenter"
"time"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -28,6 +30,24 @@ func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic
}
func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
if len(req.Username) < 3 || len(req.Password) > 20 || len(req.Password) < 8 || len(req.Password) > 20 {
return nil, errors.New("the information is illegal")
}
return &types.LoginResp{}, nil
res, err := l.svcCtx.UserRpc.Login(l.ctx, &usercenter.LoginReq{
Username: req.Username,
Passwd: req.Password,
})
if err != nil {
logx.Errorf("rpc login err: %v", err)
return nil, errors.New("login fail")
}
return &types.LoginResp{
UserId: res.Id,
Username: res.Username,
Email: res.Email,
Token: res.Token,
Expires: int64((7 * 24 * time.Hour).Seconds()),
}, nil
}
@@ -5,9 +5,11 @@ package user
import (
"context"
"errors"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"juwan-backend/app/users/rpc/usercenter"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -28,7 +30,14 @@ func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogi
}
func (l *LogoutLogic) Logout(req *types.LogoutReq) (resp *types.LogoutResp, err error) {
// todo: add your logic here and delete this line
if req.UserId <= 0 {
return nil, errors.New("invalid userId")
}
return
_, err = l.svcCtx.UserRpc.Logout(l.ctx, &usercenter.LogoutReq{UserId: req.UserId})
if err != nil {
return nil, err
}
return &types.LogoutResp{Message: "logout success"}, nil
}
@@ -6,13 +6,15 @@ package user
import (
"context"
"errors"
"juwan-backend/app/users/api/internal/contextx"
"regexp"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"juwan-backend/app/users/rpc/pb"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/utils"
"github.com/google/uuid"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -31,40 +33,54 @@ func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Register
}
}
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")
}
// 生成用户ID
userId, err := uuid.NewRandom()
if err != nil {
return nil, errors.New("generate user ID failed")
}
// 加密密码
hashedPassword, err := utils.HashPassword(req.Password)
if err != nil {
return nil, errors.New("hash password failed")
}
// 创建新用户
_res, err := l.svcCtx.UserRpc.AddUsers(l.ctx, &pb.AddUsersReq{
UserId: userId.String(),
Username: req.Username,
Passwd: hashedPassword,
Phone: req.Phone,
State: true,
requestId, err := contextx.RequestIdFrom(l.ctx)
if err != nil {
logx.Errorf("contextx.RequestIdFrom failed: %v", errjA)
return nil, errors.New("contextx.RequestIdFrom failed")
}
_, 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 {
l.Errorf("AddUsers failed: %v", err)
return nil, errors.New("add user failed")
logx.Error("failed to register user: ", err)
return nil, errors.New("failed to register user")
}
// 返回响应
return &types.RegisterResp{}, nil
return &types.RegisterResp{
UserId: 0,
Username: req.Username,
Email: req.Email,
Message: "register success",
}, nil
}