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 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 backend/auth/middleware.go (limited to 'backend/auth/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) + } + } +} -- cgit v1.2.3-70-g09d2