Add JWT token handling to document viewer and related components

This commit is contained in:
Leon Bösche
2026-01-10 05:00:18 +01:00
parent b381a46483
commit 941d8bf736
6 changed files with 42 additions and 26 deletions

View File

@@ -68,6 +68,7 @@ type contextKey string
const (
userKey contextKey = "user"
sessionKey contextKey = "session"
tokenKey contextKey = "token"
orgKey contextKey = "org"
)
@@ -83,6 +84,12 @@ func GetSession(ctx context.Context) (*database.Session, bool) {
return session, ok
}
// GetToken retrieves the JWT token from the request context
func GetToken(ctx context.Context) (string, bool) {
token, ok := ctx.Value(tokenKey).(string)
return token, ok
}
// Auth middleware
func Auth(jwtManager *jwt.Manager, db *database.DB) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
@@ -102,6 +109,7 @@ func Auth(jwtManager *jwt.Manager, db *database.DB) func(http.Handler) http.Hand
ctx := context.WithValue(r.Context(), userKey, claims.UserID)
ctx = context.WithValue(ctx, sessionKey, session)
ctx = context.WithValue(ctx, tokenKey, tokenString)
next.ServeHTTP(w, r.WithContext(ctx))
})
}