github.com/clerkinc/clerk-sdk-go@v1.49.1/examples/middleware/main.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "log" 7 "net/http" 8 9 "github.com/clerkinc/clerk-sdk-go/clerk" 10 ) 11 12 func returnActiveSession(w http.ResponseWriter, req *http.Request) { 13 sessionClaims, ok := clerk.SessionFromContext(req.Context()) 14 if ok { 15 jsonResp, _ := json.Marshal(sessionClaims) 16 fmt.Fprintf(w, string(jsonResp)) 17 } else { 18 // handle non-authenticated user 19 } 20 21 } 22 23 func main() { 24 fmt.Print("Clerk secret key: ") 25 var apiKey string 26 fmt.Scanf("%s", &apiKey) 27 28 client, err := clerk.NewClient(apiKey) 29 if err != nil { 30 panic(err) 31 } 32 33 mux := http.NewServeMux() 34 injectActiveSession := clerk.WithSessionV2(client) 35 mux.Handle("/session", injectActiveSession(http.HandlerFunc(returnActiveSession))) 36 37 err = http.ListenAndServe(":3000", mux) 38 log.Fatal(err) 39 }