Skip to content

Commit

Permalink
feat(middleware): do not check if get
Browse files Browse the repository at this point in the history
  • Loading branch information
vit0rr committed Oct 22, 2024
1 parent 49624c6 commit 1812323
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/joho/godotenv"
urlshort "github.com/vit0rr/short-spot/api/internal/url-short"
"github.com/vit0rr/short-spot/pkg/deps"
"github.com/vit0rr/short-spot/pkg/log"
"github.com/vit0rr/short-spot/pkg/telemetry"
"go.mongodb.org/mongo-driver/mongo"
)
Expand Down Expand Up @@ -52,7 +53,7 @@ func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With")

if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
Expand All @@ -67,8 +68,14 @@ func authMiddleware(next http.Handler) http.Handler {
godotenv.Load()

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
next.ServeHTTP(w, r)
return
}

token := r.Header.Get("Authorization")
if token != os.Getenv("AUTH_TOKEN") {
log.Error(r.Context(), "Unauthorized. Please provide a valid auth token")
http.Error(w, "Unauthorized. Please provide a valid auth token", http.StatusUnauthorized)
return
}
Expand Down

0 comments on commit 1812323

Please sign in to comment.