kinda working
This commit is contained in:
28
middleware_auth.go
Normal file
28
middleware_auth.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user