blob: aca1648424b640e32ab352e62bf551e89a4ab54e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)
}
}
}
|