dgrijalva / jwt-go

ARCHIVE - Golang implementation of JSON Web Tokens (JWT). This project is now maintained at:
https://github.com/golang-jwt/jwt
MIT License
10.78k stars 997 forks source link

Create token example doesn't works #450

Open victorabarros opened 3 years ago

victorabarros commented 3 years ago

Hi guys!

I'm having some problem in try create token following:

    // Create the token
    token := jwt.New(jwt.GetSigningMethod("HS256"))
    // Set some claims
    token.Claims["foo"] = "bar"
    token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
    // Sign and get the complete encoded token as a string
    tokenString, err := token.SignedString(mySigningKey)
go: finding module for package github.com/dgrijalva/jwt-go
go: downloading github.com/dgrijalva/jwt-go v1.0.2
go: downloading github.com/dgrijalva/jwt-go v3.2.0+incompatible
go: found github.com/dgrijalva/jwt-go in github.com/dgrijalva/jwt-go v3.2.0+incompatible
./prog.go:12:14: invalid operation: token.Claims["foo"] (type jwt.Claims does not support indexing)

The issue https://github.com/dgrijalva/jwt-go/issues/388#issue-579523248 helps to solve, but I think would be better if the example from documentation works too.

WhyNotHugo commented 3 years ago

It looks like the API changed several years ago, but none of the documentation has been updated since.

If anyone has examples for how the new API works, that's be super useful. I'm not sure if versions > 1.0.2 are considered usable though, maybe they're still experimental?

victorabarros commented 3 years ago

https://github.com/dgrijalva/jwt-go/issues/388#issue-579523248 helps?