fix: enforce authentication by redirecting unauthenticated users to login

This commit is contained in:
2026-05-13 15:23:37 +02:00
parent 037137f48a
commit 6f1b4db4f5

View File

@@ -2,19 +2,34 @@ package middleware
import (
"mal/internal/domain"
"net/http"
"github.com/gin-gonic/gin"
)
func AuthMiddleware(svc domain.AuthService) gin.HandlerFunc {
return func(c *gin.Context) {
// Allow access to login and logout endpoints without authentication
if c.Request.URL.Path == "/login" || c.Request.URL.Path == "/logout" {
c.Next()
return
}
sessionID, err := c.Cookie("session_id")
if err == nil {
if err != nil {
c.Redirect(http.StatusSeeOther, "/login")
c.Abort()
return
}
user, err := svc.ValidateSession(c.Request.Context(), sessionID)
if err == nil {
if err != nil {
c.Redirect(http.StatusSeeOther, "/login")
c.Abort()
return
}
c.Set("User", user)
}
}
c.Next()
}
}