Skip to content

feat(guard): add guard config option to control auth guard used for logging#31

Merged
jfrosorio merged 1 commit into
mainfrom
feat-30
Jul 15, 2026
Merged

feat(guard): add guard config option to control auth guard used for logging#31
jfrosorio merged 1 commit into
mainfrom
feat-30

Conversation

@jfrosorio

Copy link
Copy Markdown
Contributor

Closes #30

The LogApiRequest middleware resolved the user via auth()->user(), i.e. the request's default guard. When no auth:<guard> middleware had called shouldUse(), this silently fell back to config('auth.defaults.guard') (typically web), which may not be the guard the application authenticates its API with.

This PR adds a guard option to config/api-logs.php:

  • 'guard' => null (default) keeps the current behavior — the request's default guard is used.
  • Setting a guard name (e.g. 'sanctum') pins the guard used to resolve the user a logged request is attributed to.

Includes feature tests covering both the configured-guard-authenticated and configured-guard-unauthenticated cases, plus README documentation.

@jfrosorio
jfrosorio requested a review from Copilot July 15, 2026 06:56
@jfrosorio jfrosorio self-assigned this Jul 15, 2026
@jfrosorio jfrosorio added the enhancement New feature or request label Jul 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an api-logs.guard configuration option to control which Laravel auth guard is used by the LogApiRequest middleware when resolving the authenticated user (“causer”) for an API log entry, addressing cases where the default guard may not reflect the API’s authentication guard.

Changes:

  • Added guard option (default null) to config/api-logs.php to optionally pin the guard used for user resolution.
  • Updated LogApiRequest middleware to resolve the user via the configured guard.
  • Added feature tests and README documentation covering configured-guard behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/Http/Middleware/LogApiRequest.php Uses config('api-logs.guard') to select the auth guard for resolving the user before logging.
config/api-logs.php Introduces documented guard config option with default null to preserve current behavior.
tests/Feature/LogApiRequestMiddlewareTest.php Adds tests verifying the configured guard is used and that logs are skipped when that guard is unauthenticated.
README.md Documents the new guard config option and updates the config publishing section accordingly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jfrosorio
jfrosorio merged commit f050719 into main Jul 15, 2026
10 checks passed
@jfrosorio
jfrosorio deleted the feat-30 branch July 15, 2026 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add guard config option to control which auth guard is used for logging

2 participants