Created
June 21, 2019 14:49
-
-
Save gufranmirza/48b20374863410f917f022bc501445bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"time" | |
"github.com/dgrijalva/jwt-go" | |
) | |
func generateTokenPair() (map[string]string, error) { | |
// Create token | |
token := jwt.New(jwt.SigningMethodHS256) | |
// Set claims | |
// This is the information which frontend can use | |
// The backend can also decode the token and get admin etc. | |
claims := token.Claims.(jwt.MapClaims) | |
claims["sub"] = 1 | |
claims["name"] = "Jon Doe" | |
claims["admin"] = true | |
claims["exp"] = time.Now().Add(time.Minute * 15).Unix() | |
// Generate encoded token and send it as response. | |
// The signing string should be secret (a generated UUID works too) | |
t, err := token.SignedString([]byte("secret")) | |
if err != nil { | |
return nil, err | |
} | |
refreshToken := jwt.New(jwt.SigningMethodHS256) | |
rtClaims := refreshToken.Claims.(jwt.MapClaims) | |
rtClaims["sub"] = 1 | |
rtClaims["exp"] = time.Now().Add(time.Hour * 24).Unix() | |
rt, err := refreshToken.SignedString([]byte("secret")) | |
if err != nil { | |
return nil, err | |
} | |
return map[string]string{ | |
"access_token": t, | |
"refresh_token": rt, | |
}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment