go-chi / jwtauth

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

How set expires time in MapClaims #45

Closed rof20004 closed 4 years ago

rof20004 commented 4 years ago

Hi all,

I am using chi for tests purposes, but I did not found any example that validates a jwt.MapClaims with expiration time.

I am using this:

var TokenAuth = jwtauth.New(JWTAlgorithm, JWTSecret, nil)

// Transaction paths
r.Group(func(r chi.Router) {
    r.Use(jwtauth.Verifier(TokenAuth))
    r.Use(jwtauth.Authenticator)
    _transactionHttpHandler.NewTransactionHandler(r, transactionUsecase)
})

And generate JWT with this:


func generateJWT(c *model.Client) (token string, err error) {
    _, token, err = TokenAuth.Encode(jwt.MapClaims{"clientId": c.ID.String()})
    if err != nil {
        return "", err
    }

    return token, nil
}

But my token has no expires time and I do not know to do this with this package. Someone can help?

Thanks :)

gmhafiz commented 4 years ago

To anyone wondering:

claims := jwt.MapClaims {
        "email": "example@example.com,
}
jwtauth.SetExpiry(claims, time.Now().Add(time.Minute * 5))
_, tokenString, _ := s.App.Jwt().Encode(claims)