Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is goth compatible with chi? #457

Closed
darienmiller88 opened this issue May 28, 2022 · 2 comments
Closed

Is goth compatible with chi? #457

darienmiller88 opened this issue May 28, 2022 · 2 comments

Comments

@darienmiller88
Copy link

I'm having an issue where goth is not pulling the value of the {provider} parameter from my chi router, but it does so with the gorilla/pat router. Here is a code sample:

func main(){
	router := chi.NewRouter()
	patRouter := pat.New()

	store := sessions.NewCookieStore([]byte(os.Getenv("JWT_SECRET")))
	store.MaxAge(5000)
	store.Options.Path = "/"
	store.Options.HttpOnly = true   
	store.Options.Secure = true

	gothic.Store = store
	goth.UseProviders(
		google.New(os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET"), "http://localhost:8080/auth/google/callback", "email", "profile"),
	)

	router.Use(newCors.Handler)
	patRouter.Use(newCors.Handler)

        router.Get("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) {
		url, err := gothic.GetAuthURL(res, req)

		fmt.Println("url:", req.URL.Query())//Here, the map is empty.
		fmt.Println("err:", err)//Also, the err returned is " you must select a provider".
		
		res.Write([]byte(url))
	})
	
        patRouter.Get("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) {
		url, err := gothic.GetAuthURL(res, req)

		fmt.Println("url:", req.URL.Query())//Here on the other hand, the map is populated as such ->  map[:provider:[google]]
		fmt.Println("err:", err)//err is nil here.

		res.Write([]byte(url))
	})
       
        go http.ListenAndServe(":9090", patRouter)
	http.ListenAndServe(":8080", router)
}

I'm testing this from a react front using the following code:

const responsePat = await fetch("http://localhost:9090/auth/google")
const resultPat = await responsePat.text()
console.log(resultPat)//Url is returned here

const responseChi = await fetch("http://localhost:8080/auth/google")
const resultChi = await responseChi.text()
console.log(resultChi)//Empty string is returned here.

Chi and Pat both use net/http, so I'm not sure why the url parameter is not being read by the gothic.GetAuthURL(res, req) function. Am I doing anything wrong?

@noellimx
Copy link

noellimx commented Jun 12, 2022

Get provider by custom function using chi with gothic

See https://pkg.go.dev/github.com/markbates/goth/gothic#GetProviderName

  1. Define how you want to get the param
    `
    func CustomGetProviderNameFromRequestWithChaiFramework(r *http.Request) (string, error) {

    str := chi.URLParam(r, "provider")

    if str == "" {
    return "", errors.New("orovider not found")

    }

    return str, nil
    }

`

  1. Assign to gothic.GetProviderName

    gothic.GetProviderName = CustomGetProviderNameFromRequestWithChaiFramework

@spy16
Copy link

spy16 commented Oct 1, 2022

You can also make a middleware and wrap your handlers:

func withProvider(handler http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		r = gothic.GetContextWithProvider(r, chi.URLParam(r, "provider"))
		handler.ServeHTTP(w, r)
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants