Skip to content

Commit

Permalink
Convert RequireSessionV2 to a proper middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
agis committed Oct 20, 2021
1 parent f02eb5a commit 22c57b2
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions clerk/middleware_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import (

// RequireSessionV2 will hijack the request and return an HTTP status 403
// if the session is not authenticated.
func RequireSessionV2(client Client, next http.Handler) http.Handler {
f := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
claims, ok := r.Context().Value(ActiveSessionClaims).(*SessionClaims)
if !ok || claims == nil {
w.WriteHeader(http.StatusForbidden)
return
}

next.ServeHTTP(w, r)
})

return WithSessionV2(client)(f)
func RequireSessionV2(client Client) func(handler http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
f := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
claims, ok := r.Context().Value(ActiveSessionClaims).(*SessionClaims)
if !ok || claims == nil {
w.WriteHeader(http.StatusForbidden)
return
}

next.ServeHTTP(w, r)
})

return WithSessionV2(client)(f)
}
}

// SessionFromContext returns the session's (if any) claims, as parsed from the
Expand Down

0 comments on commit 22c57b2

Please sign in to comment.