BrainMinder/cmd/web/context.go
2024-08-22 10:13:16 +02:00

29 lines
571 B
Go

package main
import (
"context"
"net/http"
"brainminder.speedtech.it/internal/database"
)
type contextKey string
const (
authenticatedUserContextKey = contextKey("authenticatedUser")
)
func contextSetAuthenticatedUser(r *http.Request, user *database.User) *http.Request {
ctx := context.WithValue(r.Context(), authenticatedUserContextKey, user)
return r.WithContext(ctx)
}
func contextGetAuthenticatedUser(r *http.Request) *database.User {
user, ok := r.Context().Value(authenticatedUserContextKey).(*database.User)
if !ok {
return nil
}
return user
}