Skip to content

Instantly share code, notes, and snippets.

@cmnstmntmn
Created August 4, 2018 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmnstmntmn/6825de46a7ab1d6ebddc9cb7a1559d68 to your computer and use it in GitHub Desktop.
Save cmnstmntmn/6825de46a7ab1d6ebddc9cb7a1559d68 to your computer and use it in GitHub Desktop.
package middlewares
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func SetJwtMiddlewares(g *echo.Group) {
g.Use(middleware.JWTWithConfig(middleware.JWTConfig{
SigningMethod: "HS512",
SigningKey: []byte("mySecret"),
TokenLookup: "cookie:JWTCookie",
}))
}
@cmnstmntmn
Copy link
Author

cmnstmntmn commented Aug 4, 2018

the token is generated this way:

	// Set custom claims
	claims := &JwtCustomClaims{
		origin.ID,
		origin.Role,
		jwt.StandardClaims{
			ExpiresAt: time.Now().Add(time.Hour * 72).Unix(),
		},
	}

	// Create token with claims
	token := jwt.NewWithClaims(
	jwt.SigningMethodHS512, claims)

i create the cookie this way:

  // Set cookie
  jwtCookie := &http.Cookie{}

  jwtCookie.Name = "JWTCookie"
  jwtCookie.Value = userLogged.Jwt
  jwtCookie.Path = "/"
  jwtCookie.Expires = time.Now().Add(48 * time.Hour)

  c.SetCookie(jwtCookie)	

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment