34 lines
1.0 KiB
Go
34 lines
1.0 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"gardomatic.kleiax.de/internal/storage"
|
|
)
|
|
|
|
type contextKey string
|
|
|
|
const authenticatedUserContextKey = contextKey("authenticatedUser")
|
|
const gardenMemberContextKey = contextKey("gardenMember")
|
|
|
|
func (app *application) contextSetAuthenticatedUser(r *http.Request, user storage.User) *http.Request {
|
|
ctx := context.WithValue(r.Context(), authenticatedUserContextKey, user)
|
|
return r.WithContext(ctx)
|
|
}
|
|
|
|
func (app *application) contextGetAuthenticatedUser(r *http.Request) (storage.User, bool) {
|
|
user, ok := r.Context().Value(authenticatedUserContextKey).(storage.User)
|
|
return user, ok
|
|
}
|
|
|
|
func (app *application) contextSetGardenMember(r *http.Request, member storage.GardenMember) *http.Request {
|
|
ctx := context.WithValue(r.Context(), gardenMemberContextKey, member)
|
|
return r.WithContext(ctx)
|
|
}
|
|
|
|
func (app *application) contextGetGardenMember(r *http.Request) (storage.GardenMember, bool) {
|
|
member, ok := r.Context().Value(gardenMemberContextKey).(storage.GardenMember)
|
|
return member, ok
|
|
}
|