refactor: remove global auth service pattern
This commit is contained in:
@@ -3,19 +3,12 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"mal/api/auth"
|
"mal/api/auth"
|
||||||
ctxpkg "mal/internal/context"
|
ctxpkg "mal/internal/context"
|
||||||
"mal/internal/db"
|
"mal/internal/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
var authSvc *auth.Service
|
|
||||||
|
|
||||||
func InitAuth(service *auth.Service) {
|
|
||||||
authSvc = service
|
|
||||||
}
|
|
||||||
|
|
||||||
func Auth(authService *auth.Service) func(http.Handler) http.Handler {
|
func Auth(authService *auth.Service) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -37,42 +30,6 @@ func Auth(authService *auth.Service) func(http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RequireAuth(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
cookie, err := r.Cookie("session_id")
|
|
||||||
if err != nil {
|
|
||||||
if strings.HasPrefix(r.URL.Path, "/api/") {
|
|
||||||
w.Header().Set("HX-Redirect", "/login")
|
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
||||||
} else {
|
|
||||||
http.Redirect(w, r, "/login", http.StatusFound)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if authSvc != nil {
|
|
||||||
user, err := authSvc.ValidateSession(r.Context(), cookie.Value)
|
|
||||||
if err == nil {
|
|
||||||
ctx := context.WithValue(r.Context(), ctxpkg.UserKey, user)
|
|
||||||
r = r.WithContext(ctx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
} else {
|
|
||||||
http.Redirect(w, r, "/login", http.StatusFound)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetUser(ctx context.Context) *db.User {
|
func GetUser(ctx context.Context) *db.User {
|
||||||
user, ok := ctx.Value(ctxpkg.UserKey).(*db.User)
|
user, ok := ctx.Value(ctxpkg.UserKey).(*db.User)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -68,8 +68,6 @@ func NewRouter(cfg Config) http.Handler {
|
|||||||
watchlistSvc := watchlist.NewService(cfg.DB, cfg.SQLDB, cfg.JikanClient)
|
watchlistSvc := watchlist.NewService(cfg.DB, cfg.SQLDB, cfg.JikanClient)
|
||||||
watchlistHandler := watchlist.NewHandler(watchlistSvc)
|
watchlistHandler := watchlist.NewHandler(watchlistSvc)
|
||||||
|
|
||||||
middleware.InitAuth(cfg.AuthService)
|
|
||||||
|
|
||||||
animeSvc := anime.NewService(cfg.JikanClient, cfg.DB)
|
animeSvc := anime.NewService(cfg.JikanClient, cfg.DB)
|
||||||
animeHandler := anime.NewHandler(animeSvc)
|
animeHandler := anime.NewHandler(animeSvc)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user