aides-repo-api/internal/middlewares/auth.go

25 lines
651 B
Go
Raw Normal View History

2024-12-12 13:27:07 +00:00
package middlewares
import (
"net/http"
2024-12-15 06:36:33 +00:00
"github.com/go-chi/render"
"code.aides.space/aides-infra/aides-repo-api/internal/common/errors"
"code.aides.space/aides-infra/aides-repo-api/internal/config"
2024-12-12 13:27:07 +00:00
)
func CreateAuthGuard(cfg *config.Config) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Получаем значение из заголовка
token := r.Header.Get("Authorization")
if token != "Bearer "+cfg.Token {
render.Render(w, r, errors.ErrUnauthorized())
return
}
next.ServeHTTP(w, r)
})
}
}