♻️ refactor(auth): move UserContextKey from pkg/greet to pkg/auth
Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
30
pkg/auth/context.go
Normal file
30
pkg/auth/context.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user