fusenapi/home-user-auth/internal/logic/userloginlogic.go

63 lines
1.4 KiB
Go
Raw Normal View History

2023-06-01 10:35:09 +00:00
package logic
import (
"context"
"log"
"fusenapi/home-user-auth/internal/svc"
"fusenapi/home-user-auth/internal/types"
"fusenapi/model"
"github.com/golang-jwt/jwt/v4"
"github.com/zeromicro/go-zero/core/logx"
)
type UserLoginLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
return &UserLoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UserLoginLogic) getJwtToken(secretKey string, iat, seconds, userId int64) (string, error) {
claims := make(jwt.MapClaims)
claims["exp"] = iat + seconds
claims["iat"] = iat
claims["userId"] = userId
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = claims
return token.SignedString([]byte(secretKey))
}
func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
data, err := l.svcCtx.FsUserModel.FindOneByEmail(l.ctx, req.Name)
// logx.Info(err.Error())
log.Printf("%t %t %v", err, model.ErrNotFound, err == model.ErrNotFound)
if err == model.ErrNotFound {
// logx.Error(err)
resp = &types.Response{
Code: 304,
Message: "fail",
}
return resp, nil
}
resp = &types.Response{
Code: 200,
Message: "success",
Data: &types.DataUserLogin{
Token: data.PasswordResetToken.String,
},
}
return resp, nil
}