♻️ refactor(auth): move UserContextKey from pkg/greet to pkg/auth #90

Merged
arcodange merged 1 commits from vibe/batch-pr-d1-move-user-context-key into main 2026-05-06 06:54:37 +02:00
3 changed files with 34 additions and 18 deletions
Showing only changes of commit 92e53a6801 - Show all commits

30
pkg/auth/context.go Normal file
View 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
}

View File

@@ -3,31 +3,17 @@ package greet
import (
"context"
"dance-lessons-coach/pkg/user"
"dance-lessons-coach/pkg/auth"
"github.com/rs/zerolog/log"
)
// Context key for storing authenticated user
type contextKey string
const (
// UserContextKey is the context key for storing authenticated user
UserContextKey contextKey = "authenticatedUser"
)
type Service struct{}
func NewService() *Service {
return &Service{}
}
// GetAuthenticatedUserFromContext extracts the authenticated user from context
func GetAuthenticatedUserFromContext(ctx context.Context) (*user.User, bool) {
user, ok := ctx.Value(UserContextKey).(*user.User)
return user, ok
}
// Greet returns a greeting message for the given name.
// If name is empty, it checks for authenticated user and uses their username.
// If no authenticated user and no name, it defaults to "world".
@@ -37,7 +23,7 @@ func (s *Service) Greet(ctx context.Context, name string) string {
// If no name provided, check for authenticated user
if name == "" {
if authenticatedUser, ok := GetAuthenticatedUserFromContext(ctx); ok {
if authenticatedUser, ok := auth.GetAuthenticatedUserFromContext(ctx); ok {
name = authenticatedUser.Username
log.Trace().Ctx(ctx).Str("authenticated_user", name).Msg("Using authenticated username for greeting")
}

View File

@@ -4,7 +4,7 @@ import (
"context"
"net/http"
"dance-lessons-coach/pkg/greet"
"dance-lessons-coach/pkg/auth"
"dance-lessons-coach/pkg/user"
"github.com/rs/zerolog/log"
@@ -54,7 +54,7 @@ func (m *AuthMiddleware) Middleware(next http.Handler) http.Handler {
}
// Add user to context
ctxWithUser := context.WithValue(ctx, greet.UserContextKey, validatedUser)
ctxWithUser := context.WithValue(ctx, auth.UserContextKey, validatedUser)
r = r.WithContext(ctxWithUser)
// Continue to next handler