From aac4e9ccdebe52c156506d1899d5a38e99366f69 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 29 Nov 2025 16:59:33 +0900 Subject: refactor(backend): move middleware.go to auth package --- backend/auth/middleware.go | 24 ++++++++++++++++++++++++ backend/cmd_serve.go | 3 +-- backend/middleware.go | 25 ------------------------- 3 files changed, 25 insertions(+), 27 deletions(-) create mode 100644 backend/auth/middleware.go delete mode 100644 backend/middleware.go diff --git a/backend/auth/middleware.go b/backend/auth/middleware.go new file mode 100644 index 0000000..aca1648 --- /dev/null +++ b/backend/auth/middleware.go @@ -0,0 +1,24 @@ +package auth + +import ( + "github.com/labstack/echo/v4" + appcontext "undef.ninja/x/feedaka/context" +) + +// SessionAuthMiddleware validates session and adds user info to context +func SessionAuthMiddleware(sessionConfig *SessionConfig) echo.MiddlewareFunc { + return func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + // Try to get user ID from session + userID, err := sessionConfig.GetUserID(c) + if err == nil { + // Add user ID to context + ctx := appcontext.SetUserID(c.Request().Context(), userID) + c.SetRequest(c.Request().WithContext(ctx)) + } + // If no valid session, continue without authentication + + return next(c) + } + } +} diff --git a/backend/cmd_serve.go b/backend/cmd_serve.go index a0a2d75..f7b99ca 100644 --- a/backend/cmd_serve.go +++ b/backend/cmd_serve.go @@ -182,8 +182,7 @@ func runServe(database *sql.DB, cfg *config.Config) { // GraphQL endpoints with authentication middleware graphqlGroup := e.Group("/graphql") - graphqlGroup.Use(SessionAuthMiddleware(sessionConfig)) - // !!! ここで echo.Context を GraphQL へ渡している意味は? + graphqlGroup.Use(auth.SessionAuthMiddleware(sessionConfig)) graphqlGroup.POST("", func(c echo.Context) error { // Add Echo context to GraphQL context ctx := context.WithValue(c.Request().Context(), "echo", c) diff --git a/backend/middleware.go b/backend/middleware.go deleted file mode 100644 index 13234df..0000000 --- a/backend/middleware.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "github.com/labstack/echo/v4" - "undef.ninja/x/feedaka/auth" - appcontext "undef.ninja/x/feedaka/context" -) - -// SessionAuthMiddleware validates session and adds user info to context -func SessionAuthMiddleware(sessionConfig *auth.SessionConfig) echo.MiddlewareFunc { - return func(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - // Try to get user ID from session - userID, err := sessionConfig.GetUserID(c) - if err == nil { - // Add user ID to context - ctx := appcontext.SetUserID(c.Request().Context(), userID) - c.SetRequest(c.Request().WithContext(ctx)) - } - // If no valid session, continue without authentication - - return next(c) - } - } -} -- cgit v1.2.3-70-g09d2