29 lines
672 B
Go
29 lines
672 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/leandrofars/oktopus/internal/api/auth"
|
|
"golang.org/x/net/context"
|
|
"net/http"
|
|
)
|
|
|
|
func Middleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
tokenString := r.Header.Get("Authorization")
|
|
if tokenString == "" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
email, err := auth.ValidateToken(tokenString)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
ctx := context.WithValue(r.Context(), "email", email)
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
},
|
|
)
|
|
}
|