-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.go
31 lines (28 loc) · 909 Bytes
/
middlewares.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package rest
import (
"fmt"
"net/http"
"strings"
"github.com/devisions/go-mux-jwt-gorm-starter/api/rest/responses"
"github.com/devisions/go-mux-jwt-gorm-starter/api/rest/tokens"
"github.com/devisions/go-mux-jwt-gorm-starter/app"
"github.com/dgrijalva/jwt-go"
)
func JWTMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tokenString := r.Header.Get("Authorization")
if len(tokenString) == 0 {
responses.RespondJsonWithUnauthorizedError(w)
return
}
tokenString = strings.Replace(tokenString, "Bearer ", "", 1)
claims, err := tokens.VerifyToken(tokenString)
if err != nil {
responses.RespondJsonWithErrorReason(w, http.StatusUnauthorized, app.ErrJWTValidation, err.Error())
return
}
userId := fmt.Sprintf("%v", claims.(jwt.MapClaims)["user_id"])
r.Header.Set("userId", userId)
next.ServeHTTP(w, r)
})
}