kinda working

This commit is contained in:
wagslane
2023-05-30 21:28:23 -06:00
parent 7d1852d380
commit 80ec635705
91 changed files with 13103 additions and 2 deletions

28
middleware_auth.go Normal file
View File

@@ -0,0 +1,28 @@
package main
import (
"net/http"
"github.com/bootdotdev/learn-cicd-starter/internal/auth"
"github.com/bootdotdev/learn-cicd-starter/internal/database"
)
type authedHandler func(http.ResponseWriter, *http.Request, database.User)
func (cfg *apiConfig) middlewareAuth(handler authedHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
apiKey, err := auth.GetAPIKey(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find api key")
return
}
user, err := cfg.DB.GetUserByAPIKey(r.Context(), apiKey)
if err != nil {
respondWithError(w, http.StatusNotFound, "Couldn't get user")
return
}
handler(w, r, user)
}
}