Add JWT token handling to document viewer and related components
This commit is contained in:
@@ -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))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user