55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"juwan-backend/app/users/rpc/internal/svc"
|
|
utils2 "juwan-backend/app/users/rpc/internal/utils"
|
|
"juwan-backend/app/users/rpc/pb"
|
|
"juwan-backend/common/utils"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type LoginLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
|
|
return &LoginLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *LoginLogic) Login(in *pb.LoginReq) (*pb.LoginResp, error) {
|
|
user, err := l.svcCtx.UsersModelRO.FindOneByUsername(l.ctx, in.Username)
|
|
if err != nil {
|
|
logx.WithContext(l.ctx).Errorf("LoginLogic.Login error:%v", err)
|
|
return nil, err
|
|
}
|
|
if !utils.VerifyPassword(user.Passwd, in.Passwd) {
|
|
logx.WithContext(l.ctx).Errorf("User %s Login failed", user.Username)
|
|
return nil, errors.New("incorrect password")
|
|
}
|
|
|
|
token, err := l.svcCtx.JwtManager.New(l.ctx, &utils2.TokenPayload{
|
|
UserId: user.UserId,
|
|
IsAdmin: false,
|
|
})
|
|
if err != nil {
|
|
logx.Errorf("LoginLogic.Login gen jwt for user %v error:%v", user.UserId, err)
|
|
return nil, err
|
|
}
|
|
|
|
return &pb.LoginResp{
|
|
Token: token,
|
|
Username: user.Username,
|
|
Email: user.Email,
|
|
Id: user.UserId,
|
|
}, nil
|
|
}
|