Files
juwan-backend/app/users/rpc/internal/logic/validateTokenLogic.go
T
2026-02-26 02:17:07 +08:00

47 lines
975 B
Go

package logic
import (
"context"
"fmt"
"juwan-backend/app/users/rpc/internal/svc"
"juwan-backend/app/users/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
var USER_TOKEN_TEMP = "jwt:%v"
type ValidateTokenLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewValidateTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ValidateTokenLogic {
return &ValidateTokenLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *ValidateTokenLogic) ValidateToken(in *pb.ValidateTokenReq) (*pb.ValidateTokenResp, error) {
redisKey := fmt.Sprintf(USER_TOKEN_TEMP, in.UserId)
_, err := l.svcCtx.JwtManager.Valid(l.ctx, redisKey)
if err != nil {
return nil, err
}
users, err := l.svcCtx.UsersModelRO.FindOne(l.ctx, in.UserId)
if err != nil {
return nil, err
}
return &pb.ValidateTokenResp{
Valid: true,
Message: "OK",
UserId: in.UserId,
RoleType: users.RoleType,
}, nil
}