docs: add net/http usage example - #14
Conversation
|
Hey @WilliamK112, thanks for the example — the register and login flows look great! One thing to update before we merge: PR #15 (bearer token middleware) has just been merged, so the library now has token, ok := bearerToken(r)
if !ok { ... }
claims, err := svc.ValidateAccessToken(token)Could you replace that with the middleware instead? It would make the example actually demonstrate the feature: mux.HandleFunc("/protected", auth.Middleware(svc)(protectedHandler(svc)))
func protectedHandler(svc *auth.Service) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeError(w, http.StatusMethodNotAllowed, "use GET")
return
}
claims, _ := auth.ClaimsFromContext(r.Context())
writeJSON(w, http.StatusOK, map[string]any{
"message": "welcome to the protected route",
"claims": claims,
})
}
}You can also remove the local |
|
Updated the HTTP example to demonstrate the built-in middleware now that PR #15 is merged:
I ran \git diff --check\ locally. I couldn't run \go test ./...\ in this Codex environment because the Go toolchain isn't installed on PATH here. |
|
Hey @WilliamK112, one small thing before we merge: the branch is missing the middleware code from PR #15 which was merged to main after you cut this branch. That's why Could you run |
3e2912c to
3f66014
Compare
|
Done, thanks for the heads-up. I ran the requested rebase onto current The branch now includes the merged middleware code from #15, and the final PR diff is still just Local validation: I still cannot run |
|
I've added the CI workflows, it should automatically run the go tests for future PRs. Thanks! |
Summary
examples/http/main.gowith a smallnet/httpserver using the in-memory storage backendPOST /registertosvc.RegisterPOST /logintosvc.Loginand return access/refresh tokensGET /protectedto validateAuthorization: Bearer <token>before serving the routeFixes #11
Validation
auth.New,Register,Login,ValidateAccessToken, andmemory.NewInMemoryStorageAPIsgit diff --cached --checkNote: this Windows Codex shell does not have
go,gofmt, or Docker installed on PATH, so I could not rungofmtorgo test ./...locally. The file is formatted in standard gofmt style by inspection.