Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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"})
    }
}
Comment

How to send JSON Web Token (JWT Token) as header with Postman and golang

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})
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: load bmfont three with webpack 
Javascript :: jstring to char* 
Javascript :: adding a variable to a string without using + in javascript 
Javascript :: how to draw flower petals around circle javascript 
Javascript :: electron scrollbar 
Javascript :: get minutes with 2 numbers 
Javascript :: node code comment process real time 
Javascript :: turf greatcircle example 
Javascript :: nuxt vuetify google fonts undefined 
Javascript :: $set 
Javascript :: react-native-quick-scroll npm 
Javascript :: Say Hello Say Bye in javascript 
Javascript :: react conditional if localhost 
Javascript :: llamar a un segundo back 
Javascript :: Amazon VPC supporting 5 different IP address ranges and i wanted to know how wide those rangers are 
Javascript :: how to pronounce psychological 
Javascript :: asjasfawepiowjpowlklkcdlkdLkdlkskskjskknisbsbu 
Javascript :: javaascript for unliking twitter 
Javascript :: npm react router 6.0.0-alpha.2 
Javascript :: adonis select distinct inner join 
Javascript :: js resize div with mouse 
Javascript :: node js if 
Javascript :: curl node exporter 
Javascript :: check a divide condition and print msg javascript 
Javascript :: javacript srting 
Javascript :: whait till src img has loaded angular 
Javascript :: survey js go to last page 
Javascript :: javascript select element with pointer 
Javascript :: number++ * 5 
Javascript :: js try catch err.printstacktrace 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =