gin-jwt icon indicating copy to clipboard operation
gin-jwt copied to clipboard

Optional JWT token?

Open cbarraford opened this issue 5 years ago • 4 comments

I've forked this repo and added a function to make having a JWT token optional, which leaves it up to the code downstream to determine if authentication is needed. I did this because I'm using graphql, which has a single API endpoint for all API functionality. Since some of my graphql functions require authentication and some do not, I wanted to leave it up to the functions to enforce what its individual requirements are (both authentication and authorization).

Is this a feature this repo would like to have? I'll open a PR if its something the maintainers would like to have.

cbarraford avatar Apr 01 '19 06:04 cbarraford

alternatively, the other way you can solve this problem within graphql (instead of making changes to this code base), you can create two graphql endpoints, one auth'ed and one without auth. I personally, don't like the idea of needing to build two graphql endpoints (it goes against one of the core concepts of graphql of the path of the url not being meaningful).

cbarraford avatar Apr 01 '19 07:04 cbarraford

We are using GraphQL aswell with optional authentication. This would be cleanest way to solve it while keeping a single GraphQL endpoint.

Thoughts? @appleboy

twellick avatar Aug 19 '19 08:08 twellick

authMiddleware := authMiddleware.MiddlewareFunc()

r.Use(func(ctx *gin.Context) {
	// Only execute auth if header present
	if _, ok := ctx.Request.Header["Authorization"]; ok {
		authMiddleware(ctx)
	}
})

Seems to work.

cliedeman avatar Oct 27 '19 17:10 cliedeman

authMiddleware := authMiddleware.MiddlewareFunc()

r.Use(func(ctx *gin.Context) {
	// Only execute auth if header present
	if _, ok := ctx.Request.Header["Authorization"]; ok {
		authMiddleware(ctx)
	}
})

Seems to work.

@cliedeman This worked for me! Thanks!

colmex avatar Jan 02 '22 02:01 colmex