Spaces:
Sleeping
Sleeping
| package handlers | |
| import ( | |
| "net/http" | |
| "fastfileviewer/backend/internal/config" | |
| "fastfileviewer/backend/internal/middleware" | |
| "github.com/gin-gonic/gin" | |
| ) | |
| type AuthHandler struct { | |
| cfg config.Config | |
| } | |
| func NewAuthHandler(cfg config.Config) *AuthHandler { | |
| return &AuthHandler{cfg: cfg} | |
| } | |
| type loginRequest struct { | |
| Password string `json:"password"` | |
| } | |
| func (h *AuthHandler) Login(ctx *gin.Context) { | |
| var req loginRequest | |
| if err := ctx.ShouldBindJSON(&req); err != nil { | |
| ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"}) | |
| return | |
| } | |
| if req.Password != h.cfg.AdminPassword { | |
| ctx.JSON(http.StatusUnauthorized, gin.H{"error": "invalid credentials"}) | |
| return | |
| } | |
| token, err := middleware.IssueToken(h.cfg.JWTSecret, h.cfg.TokenTTL) | |
| if err != nil { | |
| ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to issue token"}) | |
| return | |
| } | |
| ctx.JSON(http.StatusOK, gin.H{"token": token}) | |
| } | |