Skip to content

Commit

Permalink
refactor middleware into own package
Browse files Browse the repository at this point in the history
  • Loading branch information
hellojebus committed Jun 8, 2018
1 parent 200e228 commit f92886c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions routerMiddleware.go → middleware/middleware.go
@@ -1,4 +1,4 @@
package main
package middleware

import (
"net/http"
Expand All @@ -8,15 +8,15 @@ import (
customHTTP "github.com/hellojebus/go-envoz-api/http"
)

func jwtMiddleware(next http.Handler) http.Handler {
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 {
customHTTP.NewErrorResponse(w, http.StatusUnauthorized, "Authentication failure")
return
}
tokenString = strings.Replace(tokenString, "Bearer ", "", 1)
claims, err := customHTTP.VerifyToken(tokenString)
claims, err := VerifyToken(tokenString)
if err != nil {
customHTTP.NewErrorResponse(w, http.StatusUnauthorized, "Error verifying JWT token: " + err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion http/jwt.go → middleware/verifyJWT.go
@@ -1,4 +1,4 @@
package http
package middleware

import (
"github.com/dgrijalva/jwt-go"
Expand Down
3 changes: 2 additions & 1 deletion router.go
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"github.com/hellojebus/go-envoz-api/user"
customRouter "github.com/hellojebus/go-envoz-api/router"
"github.com/hellojebus/go-envoz-api/middleware"
)

func NewRouter() *mux.Router {
Expand All @@ -28,7 +29,7 @@ func NewRouter() *mux.Router {

//check to see if route should be protected with jwt
if r.Protected {
handler = jwtMiddleware(r.HandlerFunc)
handler = middleware.JWTMiddleware(r.HandlerFunc)
}

//attach sub route
Expand Down

0 comments on commit f92886c

Please sign in to comment.