Last active
March 26, 2020 16:31
-
-
Save Ananto30/05c56a274e7ed14c5454724a93c74942 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 ( | |
"fmt" | |
"net/http" | |
"time" | |
"github.com/dgrijalva/jwt-go" | |
) | |
var jwtKey = []byte("my_secret_key") | |
type Claims struct { | |
Username string | |
jwt.StandardClaims | |
} | |
func HelloServer(w http.ResponseWriter, r *http.Request) { | |
expirationTime := time.Now().Add(5 * time.Minute) | |
claims := &Claims{ | |
Username: "a1b2c3", | |
StandardClaims: jwt.StandardClaims{ | |
ExpiresAt: expirationTime.Unix(), | |
}, | |
} | |
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | |
tokenString, _ := token.SignedString(jwtKey) | |
fmt.Fprintf(w, tokenString) | |
} | |
func main() { | |
http.HandleFunc("/", HelloServer) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment