I am using "github.com/dgrijalva/jwt-go", and able to send a token to my frontend, and what I would like to know how I could retrieve the token sent from the frontend so that I can verify if the token that was sent is valid and if so the secured resource will be delivered.
Here is the token sent from frontend JavaScript:
headers: {
'Authorization':'Bearer' + localStorage.getItem('id_token')
}
Here is the code to send token
token := jwt.New(jwt.GetSigningMethod("HS256"))
claims := make(jwt.MapClaims)
claims["userName"] = loginRequest.UserName
claims["exp"] = time.Now().Add(time.Minute * 60).Unix()
token.Claims = claims
tokenString, err := token.SignedString([]byte(SecretKey))
tokenByte, err := json.Marshal(data)
w.WriteHeader(201)
w.Write(tokenByte)
Here is the code to verify the token
func VerifyToken(r *http.Request) bool {
reqToken := r.Header.Get("Authorization")
token, err := jwt.Parse(reqToken, func(t *jwt.Token) (interface{}, error) {
return []byte(SecretKey), nil
})
if err == nil && token.Valid {
fmt.Println("valid token")
return true
} else {
fmt.Println("invalid token")
return false
}
}
Am getting nil token as a return, my guess is I have sent bearer and I think it might need parsing if so how?