- introduces auth package, for common (sam, crm) auth handlers - routes are now generated inside server package, each handler got its own MountRoutes(r chi.Router, middlewares ...func(http.Handler) http.Handler) - there MountRoute methods are now called from non-generated code so we can inject services to (rest) controllers
21 lines
395 B
Go
21 lines
395 B
Go
package auth
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/titpetric/factory/resputil"
|
|
"net/http"
|
|
)
|
|
|
|
func AuthenticationMiddlewareValidOnly(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var ctx = r.Context()
|
|
|
|
if !GetIdentityFromContext(ctx).Valid() {
|
|
resputil.JSON(w, errors.New("Unauthorized"))
|
|
return
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|