feat: add sqlite-backed auth and library services
Bootstrap SQLite on server startup with embedded migrations and development seed data. Replace placeholder auth and library responses with database-backed services, bearer sessions, and repository-driven API handlers.
This commit is contained in:
@@ -1,12 +1,19 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/benya/temporserv/internal/auth"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const currentUserKey contextKey = "currentUser"
|
||||
|
||||
func requestLogger(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
startedAt := time.Now()
|
||||
@@ -51,3 +58,24 @@ func cors(origins string) func(http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (a app) requireAuth(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := a.auth.CurrentUser(r.Context(), r.Header.Get("Authorization"))
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), currentUserKey, user)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func currentUserFromContext(r *http.Request) auth.User {
|
||||
user, ok := r.Context().Value(currentUserKey).(auth.User)
|
||||
if !ok {
|
||||
return auth.User{}
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user