- 移除加密相关代码(服务端和所有客户端) - 修复 JWT 验证未检查签名算法的问题 - 修复重置密码使用不安全随机数的问题 - 实现 Logout 服务端失效机制 - 修复历史版本对比 diff 颜色标记错误 - 修复占位符解析循环引用风险 - 添加用户禁用状态检查 - 更新 bug 清单文档
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"kenaito-conf/server/internal/dao"
|
|
"kenaito-conf/server/internal/middleware"
|
|
"kenaito-conf/server/internal/model"
|
|
"kenaito-conf/server/internal/service"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AuthHandler struct {
|
|
authService *service.AuthService
|
|
}
|
|
|
|
func NewAuthHandler(authService *service.AuthService) *AuthHandler {
|
|
return &AuthHandler{authService: authService}
|
|
}
|
|
|
|
type LoginRequest struct {
|
|
UserName string `json:"userName" binding:"required"`
|
|
Password string `json:"password" binding:"required"`
|
|
}
|
|
|
|
func (h *AuthHandler) Login(c *gin.Context) {
|
|
var req LoginRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.WrapperResponse{Code: 400, Msg: "参数错误"})
|
|
return
|
|
}
|
|
|
|
token, userInfo, err := h.authService.Login(req.UserName, req.Password)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, model.WrapperResponse{Code: 400, Msg: err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.WrapperResponse{
|
|
Code: 0,
|
|
Data: gin.H{"token": token, "userInfo": userInfo},
|
|
})
|
|
}
|
|
|
|
func (h *AuthHandler) GetLoginUserInfo(c *gin.Context) {
|
|
userInfo := middleware.GetUserInfo(c)
|
|
if userInfo == nil {
|
|
c.JSON(http.StatusUnauthorized, model.WrapperResponse{Code: 401, Msg: "未登录"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model.WrapperResponse{Code: 0, Data: userInfo})
|
|
}
|
|
|
|
func (h *AuthHandler) Logout(c *gin.Context) {
|
|
userInfo := middleware.GetUserInfo(c)
|
|
if userInfo != nil {
|
|
userId, _ := strconv.Atoi(userInfo.Id)
|
|
now := time.Now()
|
|
dao.Engine.ID(userId).Cols("last_logout_at").Update(&model.User{LastLogoutAt: &now})
|
|
}
|
|
c.JSON(http.StatusOK, model.WrapperResponse{Code: 0})
|
|
}
|