82 lines
2.3 KiB
Go
Raw Normal View History

2023-06-01 18:35:09 +08:00
package logic
import (
"context"
2023-06-20 17:29:02 +08:00
"errors"
2023-07-24 12:18:27 +08:00
"fmt"
"net/http"
2023-06-06 20:08:32 +08:00
"time"
2023-06-01 18:35:09 +08:00
2023-06-08 10:51:56 +08:00
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
2023-06-12 15:17:42 +08:00
"fusenapi/utils/auth"
2023-06-05 17:56:55 +08:00
"fusenapi/utils/basic"
2023-06-01 18:35:09 +08:00
"github.com/zeromicro/go-zero/core/logx"
2023-06-15 16:08:43 +08:00
"gorm.io/gorm"
2023-06-01 18:35:09 +08:00
)
type UserLoginLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
2023-07-24 12:18:27 +08:00
token string
2023-06-01 18:35:09 +08:00
}
func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
return &UserLoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
2023-07-24 12:18:27 +08:00
func (l *UserLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request) {
if l.token != "" {
w.Header().Add("Authorization", fmt.Sprintf("Bearer %s", l.token))
}
}
func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin, userinfo *auth.UserInfo) (resp *basic.Response) {
2023-06-12 15:17:42 +08:00
// 创建一个 FsUserModel 对象 m 并实例化之,该对象用于操作 MySQL 数据库中的用户数据表。
2023-06-21 14:26:29 +08:00
m := l.svcCtx.AllModels.FsUser
2023-06-12 15:17:42 +08:00
2023-06-15 16:08:43 +08:00
// 在用户数据表中根据登录名(email)查找用户记录,并返回 UserModel 类型的结构体对象 userModel。
2023-07-10 17:24:06 +08:00
user, err := m.FindUserByEmail(l.ctx, req.Email)
2023-06-20 17:29:02 +08:00
if errors.Is(err, gorm.ErrRecordNotFound) {
2023-07-24 12:18:27 +08:00
return resp.SetStatus(basic.CodeEmailNotFoundErr)
2023-06-01 18:35:09 +08:00
}
2023-06-06 20:08:32 +08:00
2023-06-12 15:17:42 +08:00
// 如果在用户数据表中找到了登录名匹配的用户记录,则判断密码是否匹配。
2023-06-15 16:08:43 +08:00
if *user.PasswordHash != req.Password {
2023-06-07 18:30:58 +08:00
logx.Info("密码错误")
2023-07-24 12:18:27 +08:00
return resp.SetStatus(basic.CodePasswordErr)
2023-06-07 18:30:58 +08:00
}
2023-06-12 15:17:42 +08:00
// 如果密码匹配,则生成 JWT Token。
2023-06-06 20:08:32 +08:00
nowSec := time.Now().Unix()
2023-07-24 12:18:27 +08:00
jwtToken, err := auth.GenerateJwtToken(&l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, user.Id, 0)
2023-06-12 15:17:42 +08:00
// 如果生成 JWT Token 失败,则抛出错误并返回未认证的状态码。
2023-06-06 20:08:32 +08:00
if err != nil {
logx.Error(err)
2023-07-24 12:18:27 +08:00
return resp.SetStatus(basic.CodeUnAuth)
2023-06-06 20:08:32 +08:00
}
2023-06-12 15:17:42 +08:00
// 如果更新 VerificationToken 字段失败,则返回未认证的状态码。
2023-06-07 18:30:58 +08:00
if err != nil {
2023-07-24 12:18:27 +08:00
return resp.SetStatus(basic.CodeUnAuth)
2023-06-07 18:30:58 +08:00
}
2023-06-12 15:17:42 +08:00
// 构造 DataUserLogin 类型的数据对象 data 并设置其属性值为生成的 JWT Token。
2023-06-05 17:56:55 +08:00
data := &types.DataUserLogin{
2023-06-07 18:30:58 +08:00
Token: jwtToken,
2023-06-01 18:35:09 +08:00
}
2023-06-06 20:08:32 +08:00
2023-07-24 12:18:27 +08:00
l.token = jwtToken
2023-06-12 15:17:42 +08:00
// 返回认证成功的状态码以及数据对象 data 和 JWT Token。
2023-07-24 12:18:27 +08:00
return resp.SetStatus(basic.CodeOK, data)
2023-06-01 18:35:09 +08:00
}