Skip to content

Instantly share code, notes, and snippets.

@agzuniverse
Created April 15, 2020 12:35
Show Gist options
  • Save agzuniverse/995ebb4724330ac3a13878787bb4737b to your computer and use it in GitHub Desktop.
Save agzuniverse/995ebb4724330ac3a13878787bb4737b to your computer and use it in GitHub Desktop.
else {
jwtToken := authHeader[1]
token, err := jwt.Parse(jwtToken, 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(SECRETKEY), nil
})
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
ctx := context.WithValue(r.Context(), "props", claims)
// Access context values in handlers like this
// props, _ := r.Context().Value("props").(jwt.MapClaims)
next.ServeHTTP(w, r.WithContext(ctx))
} else {
fmt.Println(err)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment