Skip to content

Instantly share code, notes, and snippets.

@Ananto30
Last active March 26, 2020 16:31
Show Gist options
  • Save Ananto30/05c56a274e7ed14c5454724a93c74942 to your computer and use it in GitHub Desktop.
Save Ananto30/05c56a274e7ed14c5454724a93c74942 to your computer and use it in GitHub Desktop.
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