fix: watchlist updated_at and unified auth middleware
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user