Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
31 lines
1.1 KiB
Go
31 lines
1.1 KiB
Go
// Package auth — context keys and helpers for authentication state.
|
|
//
|
|
// This file owns the symbols that other packages use to read/write the
|
|
// authenticated user from a request context. Previously these symbols
|
|
// lived in pkg/greet/ which was the wrong home (auth concern in greet
|
|
// package) ; moved here as part of the middleware design cleanup.
|
|
package auth
|
|
|
|
import (
|
|
"context"
|
|
|
|
"dance-lessons-coach/pkg/user"
|
|
)
|
|
|
|
// contextKey is unexported to prevent collisions with other packages
|
|
// using string keys (Go convention).
|
|
type contextKey string
|
|
|
|
// UserContextKey is the key under which the authenticated user is
|
|
// stored in the request context by AuthMiddleware.
|
|
const UserContextKey contextKey = "authenticatedUser"
|
|
|
|
// GetAuthenticatedUserFromContext extracts the authenticated user from
|
|
// the request context. Returns (nil, false) if no user is present
|
|
// (which is the case for unauthenticated requests AND for requests
|
|
// that failed silent fail-through ; cf. AuthMiddleware semantics).
|
|
func GetAuthenticatedUserFromContext(ctx context.Context) (*user.User, bool) {
|
|
u, ok := ctx.Value(UserContextKey).(*user.User)
|
|
return u, ok
|
|
}
|