go-chi / jwtauth

JWT authentication middleware for Go HTTP services
MIT License
543 stars 91 forks source link

add TokenFromContext convenience function #12

Closed Eriner closed 7 years ago

Eriner commented 7 years ago

I've added a convenience function for retrieving a token from a given context. I was having some issues pulling the jwtToken from a context (was always of type nil), specifically this operation:

jwtToken, ok := ctx.Value("jwt").(*jwt.Token)

Moving the Authenticator function to my own package, without edits, demonstrated the issue. Thus, I figured the easiest way to solve the problem was to create a convenience function within jwtauth to return a jwt.Token from a given context.

I've replaced some of code in the Authenticator function and the associated test to make use of this function. I've assigned the returned jwtToken to the bitbucket because we don't need it (we check the error), but I think it's important for new devs to see that the function returns the token object.

All tests are passing.

Thanks for the great project, @pkieltyka!

pkieltyka commented 7 years ago

thanks for this suggestion, its a good idea and in the refactor of jwtauth I included a similar method: https://github.com/go-chi/jwtauth/blob/master/jwtauth.go#L172