I am using JWT tokens for authentication and authorizaton process. I wish to set jwt token in "Authorization: Bearer ".
w.Header().Add("Authorization", "Bearer "+TokenString)
I have used the above line to set my token in Authorization header in my response.
I need to use the token to access my other end points.
If I try to access the other end points using this header token. I cant able to do that. I am getting error as no authorization header.
This is my login function
func (c *Customer) Login(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Method mismatch"))
return
}
customer := mappers.Decode(w, r)
if customer == nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Cannot able to parse"))
return
}
err := c.customer.CusotmerLoginService(customer)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("UNauthorised user or Register first"))
fmt.Println(err)
return
}
token, err := c.customer.GenerateToken(customer)
if err != nil || token == "" {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Cannot able to generate token"))
return
}
w.Header().Add("Authorization", "Bearer "+TokenString)
w.WriteHeader(http.StatusAccepted)
w.Write([]byte("Logined successfully"))
return
}
The following line is used to get the token from header
authHeader := r.Header.Get("Authorization")
If I use that line, I am getting error as no authorization header.
Authorization
header in subsequent requests. Note also thatAuthorization
is a request-specific header and should generally not be used in responses, sow.Header().Add("Authorization", ...
is bad style.