fix: watchlist updated_at and unified auth middleware

This commit is contained in:
2026-05-02 17:53:08 +02:00
committed by Mikkel Elvers
parent 5940d7828a
commit e5c32fd154
5 changed files with 20 additions and 21 deletions

View File

@@ -3,9 +3,6 @@ package middleware
import (
"net/http"
"strings"
"mal/internal/context"
"mal/internal/db"
)
type AccessPolicy struct {
@@ -47,7 +44,8 @@ func RequireGlobalAuthWithPolicy(policy AccessPolicy) func(http.Handler) http.Ha
return
}
user, ok := r.Context().Value(context.UserKey).(*database.User)
user := GetUser(r.Context())
ok := user != nil
if !ok || user == nil {
if strings.HasPrefix(r.URL.Path, "/api/") || r.Header.Get("HX-Request") == "true" {
w.Header().Set("HX-Redirect", "/login")

View File

@@ -58,8 +58,8 @@ func RequireAuth(next http.Handler) http.Handler {
}
}
user, ok := r.Context().Value(ctxpkg.UserKey).(*database.User)
if !ok || user == nil {
user := GetUser(r.Context())
if user == nil {
if strings.HasPrefix(r.URL.Path, "/api/") {
w.Header().Set("HX-Redirect", "/login")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
@@ -73,8 +73,8 @@ func RequireAuth(next http.Handler) http.Handler {
})
}
func GetUser(ctx interface{}) *database.User {
user, ok := ctx.(*database.User)
func GetUser(ctx context.Context) *database.User {
user, ok := ctx.Value(ctxpkg.UserKey).(*database.User)
if !ok {
return nil
}