移除管理员数据库模型,改为通过配置管理管理员身份和认证逻辑,并更新相关逻辑和文档

This commit is contained in:
2026-01-14 18:12:54 +08:00
parent 8aa0988ab8
commit 903d5b865e
5 changed files with 32 additions and 40 deletions

View File

@@ -2,10 +2,9 @@ package admin
import (
"FileRelay/internal/auth"
"FileRelay/internal/bootstrap"
"FileRelay/internal/config"
"FileRelay/internal/model"
"net/http"
"time"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
@@ -43,27 +42,24 @@ func (h *AuthHandler) Login(c *gin.Context) {
return
}
var admin model.Admin
if err := bootstrap.DB.First(&admin).Error; err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Admin not found"))
passwordHash := config.GlobalConfig.Security.AdminPasswordHash
if passwordHash == "" {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Admin password hash not configured"))
return
}
if err := bcrypt.CompareHashAndPassword([]byte(admin.PasswordHash), []byte(req.Password)); err != nil {
if err := bcrypt.CompareHashAndPassword([]byte(passwordHash), []byte(req.Password)); err != nil {
c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Incorrect password"))
return
}
token, err := auth.GenerateToken(admin.ID)
// 使用固定 ID 1 代表管理员(因为不再有数据库记录)
token, err := auth.GenerateToken(1)
if err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Failed to generate token"))
return
}
// 更新登录时间
now := time.Now()
bootstrap.DB.Model(&admin).Update("last_login", &now)
c.JSON(http.StatusOK, model.SuccessResponse(LoginResponse{
Token: token,
}))