oktopus/backend/services/controller/internal/api/middleware/middleware.go
Leandro Antônio Farias Machado bfb2acf0fa feat(api): user jwt authentication
2023-05-02 23:57:00 -03:00

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