aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/api/auth_middleware.go
blob: 97f89460a60a83dec35ef4d98338857b805897c2 (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
25
26
27
28
29
30
31
32
package api

import (
	"context"

	"github.com/labstack/echo/v4"

	"albatross-2026-backend/auth"
)

type contextKey struct{}

func JWTCookieMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		cookie, err := c.Cookie("albatross_token")
		if err != nil {
			return next(c)
		}
		claims, err := auth.ParseJWT(cookie.Value)
		if err != nil {
			return next(c)
		}
		ctx := context.WithValue(c.Request().Context(), contextKey{}, claims)
		c.SetRequest(c.Request().WithContext(ctx))
		return next(c)
	}
}

func GetJWTClaimsFromContext(ctx context.Context) (*auth.JWTClaims, bool) {
	claims, ok := ctx.Value(contextKey{}).(*auth.JWTClaims)
	return claims, ok
}