fix: watchlist updated_at and unified auth middleware
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
"mal/integrations/jikan"
|
"mal/integrations/jikan"
|
||||||
ctxpkg "mal/internal/context"
|
ctxpkg "mal/internal/context"
|
||||||
"mal/internal/db"
|
"mal/internal/db"
|
||||||
|
"mal/internal/middleware"
|
||||||
"mal/templates"
|
"mal/templates"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -85,8 +86,9 @@ func (h *Handler) HandleCatalog(w http.ResponseWriter, r *http.Request) {
|
|||||||
var cw []database.GetContinueWatchingEntriesRow
|
var cw []database.GetContinueWatchingEntriesRow
|
||||||
watchlistMap := make(map[int64]bool)
|
watchlistMap := make(map[int64]bool)
|
||||||
var watchlistIDs []int64
|
var watchlistIDs []int64
|
||||||
user, userOk := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
user := middleware.GetUser(r.Context())
|
||||||
if userOk && user != nil {
|
userOk := user != nil
|
||||||
|
if userOk {
|
||||||
cw, _ = h.db.GetContinueWatchingEntries(r.Context(), user.ID)
|
cw, _ = h.db.GetContinueWatchingEntries(r.Context(), user.ID)
|
||||||
watchlist, _ := h.db.GetUserWatchList(r.Context(), user.ID)
|
watchlist, _ := h.db.GetUserWatchList(r.Context(), user.ID)
|
||||||
watchlistIDs = make([]int64, len(watchlist))
|
watchlistIDs = make([]int64, len(watchlist))
|
||||||
@@ -111,7 +113,7 @@ func (h *Handler) HandleCatalog(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) HandleBrowse(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) HandleBrowse(w http.ResponseWriter, r *http.Request) {
|
||||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
user := middleware.GetUser(r.Context())
|
||||||
|
|
||||||
q := r.URL.Query().Get("q")
|
q := r.URL.Query().Get("q")
|
||||||
animeType := r.URL.Query().Get("type")
|
animeType := r.URL.Query().Get("type")
|
||||||
@@ -230,7 +232,7 @@ func (h *Handler) HandleAnimeDetails(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
user := middleware.GetUser(r.Context())
|
||||||
|
|
||||||
var status string
|
var status string
|
||||||
var watchlistIDs []int64
|
var watchlistIDs []int64
|
||||||
@@ -276,7 +278,7 @@ func (h *Handler) HandleHTMLWatchOrder(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
user := middleware.GetUser(r.Context())
|
||||||
watchlistMap := make(map[int64]bool)
|
watchlistMap := make(map[int64]bool)
|
||||||
if user != nil {
|
if user != nil {
|
||||||
watchlist, _ := h.db.GetUserWatchList(r.Context(), user.ID)
|
watchlist, _ := h.db.GetUserWatchList(r.Context(), user.ID)
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"mal/integrations/jikan"
|
"mal/integrations/jikan"
|
||||||
ctxpkg "mal/internal/context"
|
"mal/internal/middleware"
|
||||||
database "mal/internal/db"
|
|
||||||
"mal/templates"
|
"mal/templates"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -82,7 +81,7 @@ func (h *Handler) HandleWatchPage(w http.ResponseWriter, r *http.Request) {
|
|||||||
return episodes.Data[i].MalID < episodes.Data[j].MalID
|
return episodes.Data[i].MalID < episodes.Data[j].MalID
|
||||||
})
|
})
|
||||||
|
|
||||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
user := middleware.GetUser(r.Context())
|
||||||
|
|
||||||
currentEpID := r.URL.Query().Get("ep")
|
currentEpID := r.URL.Query().Get("ep")
|
||||||
if currentEpID == "" {
|
if currentEpID == "" {
|
||||||
@@ -238,7 +237,7 @@ func (h *Handler) HandleEpisodeData(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
episodeID := parts[5]
|
episodeID := parts[5]
|
||||||
|
|
||||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
user := middleware.GetUser(r.Context())
|
||||||
userID := ""
|
userID := ""
|
||||||
if user != nil {
|
if user != nil {
|
||||||
userID = user.ID
|
userID = user.ID
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
ctxpkg "mal/internal/context"
|
|
||||||
database "mal/internal/db"
|
database "mal/internal/db"
|
||||||
|
"mal/internal/middleware"
|
||||||
"mal/templates"
|
"mal/templates"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ func (h *Handler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
user := middleware.GetUser(r.Context())
|
||||||
if user == nil {
|
if user == nil {
|
||||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
@@ -58,7 +58,7 @@ func (h *Handler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.Request) {
|
||||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
user := middleware.GetUser(r.Context())
|
||||||
if user == nil {
|
if user == nil {
|
||||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
@@ -86,7 +86,7 @@ func (h *Handler) HandleDeleteContinueWatching(w http.ResponseWriter, r *http.Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
|
||||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
user := middleware.GetUser(r.Context())
|
||||||
if user == nil {
|
if user == nil {
|
||||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"mal/internal/context"
|
|
||||||
"mal/internal/db"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccessPolicy struct {
|
type AccessPolicy struct {
|
||||||
@@ -47,7 +44,8 @@ func RequireGlobalAuthWithPolicy(policy AccessPolicy) func(http.Handler) http.Ha
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, ok := r.Context().Value(context.UserKey).(*database.User)
|
user := GetUser(r.Context())
|
||||||
|
ok := user != nil
|
||||||
if !ok || user == nil {
|
if !ok || user == nil {
|
||||||
if strings.HasPrefix(r.URL.Path, "/api/") || r.Header.Get("HX-Request") == "true" {
|
if strings.HasPrefix(r.URL.Path, "/api/") || r.Header.Get("HX-Request") == "true" {
|
||||||
w.Header().Set("HX-Redirect", "/login")
|
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)
|
user := GetUser(r.Context())
|
||||||
if !ok || user == nil {
|
if user == nil {
|
||||||
if strings.HasPrefix(r.URL.Path, "/api/") {
|
if strings.HasPrefix(r.URL.Path, "/api/") {
|
||||||
w.Header().Set("HX-Redirect", "/login")
|
w.Header().Set("HX-Redirect", "/login")
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
@@ -73,8 +73,8 @@ func RequireAuth(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUser(ctx interface{}) *database.User {
|
func GetUser(ctx context.Context) *database.User {
|
||||||
user, ok := ctx.(*database.User)
|
user, ok := ctx.Value(ctxpkg.UserKey).(*database.User)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user