How to send JSON Web Token (JWT Token) as header with Postman and golang
func ProtectedEndpoint(w http.ResponseWriter, req *http.Request) {
params := req.URL.Query()
token, _ := jwt.Parse(params["token"][0], func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("There was an error")
}
return []byte("secret"), nil
})
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
var user User
mapstructure.Decode(claims, &user)
json.NewEncoder(w).Encode(user)
} else {
json.NewEncoder(w).Encode(Exception{Message: "Invalid authorization token"})
}
}
func CreateTokenEndpoint(w http.ResponseWriter, req *http.Request) {
var user User
_ = json.NewDecoder(req.Body).Decode(&user)
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"username": user.Username,
})
tokenString, error := token.SignedString([]byte("secret"))
if error != nil {
fmt.Println(error)
}
json.NewEncoder(w).Encode(JwtToken{Token: tokenString})
}
Also in JavaScript:
- visual studio appsettings development json not nested appsettings.json
- pushing to an array
- numbers counting up css
- cannot find module @babel/compat-data/data/corejs3-shipped-proposals
- js is object empty
- change the value in checkbox by button react
- sort javascript array
- javascript array of objects
- clear terminal node js
- jquery enforce important
- stop a setinterval
- heroku failed to run files from static folder in node
- funciones invocan a funciones javascript
- how to get whole json object with jsonpath
- javascript push item to beginning of array
- get date and change format and increase date or month in javascript jquery
- add firebase javascript
- select elements id like jquery
- click binding angular 8
- download datepicker js
- how to get the all input element id value using jquery
- check unique object in array javascript site:stackoverflow.com
- cb() never called! npm ERR! This is an error with npm itself.
- how can node js file be serve