Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Last active May 22, 2021 10:32
Show Gist options
  • Save dipeshhkc/92201dcffe709282df794906ac294bfd to your computer and use it in GitHub Desktop.
Save dipeshhkc/92201dcffe709282df794906ac294bfd to your computer and use it in GitHub Desktop.
package handler
func GenerateToken(userid uint) string {
claims := jwt.MapClaims{
"exp": time.Now().Add(time.Hour * 3).Unix(),
"iat": time.Now().Unix(),
"userID": userid,
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
t, _ := token.SignedString([]byte(os.Getenv("JWT_SECRET")))
return t
}
func ValidateToken(token string) (*jwt.Token, error) {
return jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(os.Getenv("JWT_SECRET")), nil
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment