markbates / goth

Package goth provides a simple, clean, and idiomatic way to write authentication packages for Go web applications.
https://blog.gobuffalo.io/goth-needs-a-new-maintainer-626cd47ca37b
MIT License
5.2k stars 566 forks source link

Pickup provider name from go 1.22 mux #552

Open pinpox opened 2 months ago

pinpox commented 2 months ago

Using the new mux addet to go 1.22 natively, the provider name is not picked up. It mostly replaces gorilla/pat in many places, e.g. path parameters.

There is the alternative of working around this like this:

    mux.HandleFunc("GET /auth/{provider}", func(res http.ResponseWriter, req *http.Request) {
        // try to get the user without re-authenticating

        // goth doesn't pick up the provider using the go 1.22 new routers, so
        // we work around by setting it in the context.
        req = req.WithContext(context.WithValue(req.Context(), "provider", req.PathValue("provider")))

        if gothUser, err := gothic.CompleteUserAuth(res, req); err == nil {
            err = templates.ExecuteTemplate(res, "user.html", gothUser)
            if err != nil {
                panic(err)
            }
        } else {

            gothic.BeginAuthHandler(res, req)
        }
    })

By merging this PR it would no longer be necessary to set the provider explicitely in the context of the request.