refactor: Provided an Option to Set Cookie using *GinJWTMiddleware #335
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
SetCookie
code is redundant we’ve duplicated the logic for setting cookies in both theLoginHandler
andRefreshToken
.func (*GinJWTMiddleware) TokenGenerator
method for clients to generateJWT
tokens independently of theLoginHandler
orRefreshToken
methods. However, without a dedicatedSetCookie
method, cookie setting remains tightly coupled with these methods. Introducing aSetCookie
method would enhance scalability by enabling cookies to be set independently, eliminating the need for manual cookie handling where such functionality is currently absent.Context:
This library looks very useful for us. However at the moment I don't think it supports
oauth
? Basically I don't want my users to register with a username/password, but instead I'd like them to use SSO, maybe provided by Facebook, Google or our built inhouse elitmus.com. Once the oauth flow is complete, I'd like to use my own JWT tokens though. I'd like to use the user info I get from SSO to build the token.So far I have something like this:
I am using the
TokenGenerator
method to generate thejwt
tokenAnd in our case we want to store the token in the cookie and we don't have an method to do that. As that method is tightly coupled with the
LoginHandler
method.To make the method to set the cookie for us I have written the method in our application code.
Solution Implementation:
SetCookie
Method: This method is added to the*GinJWTMiddleware
and is responsible for setting the cookie in the response. It takes the context, token as parameters.LoginHandler
andRefreshToken
: Both methods now callSetCookie
to set the cookie, eliminating the duplicated logic and centralizing cookie handling in one place.Fixes: #336