Skip to content

Skips auth context restore for sync queues #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/LaravelAuthQueueServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
namespace DatPM\LaravelAuthQueue;

use Illuminate\Queue\Queue;
use Illuminate\Queue\SyncQueue;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;
use DatPM\LaravelAuthQueue\Guards\KernelGuard;
use Illuminate\Contracts\Database\ModelIdentifier;
use DatPM\LaravelAuthQueue\Traits\WasAuthenticated;
use Illuminate\Support\Facades\Queue as QueueManager;

class LaravelAuthQueueServiceProvider extends ServiceProvider
{
public function boot()
{
Queue::createPayloadUsing(function ($connectionName, $queue, $payload) {
// Ignore adding extra payload for Sync Queue
if (QueueManager::connection($connectionName) instanceof SyncQueue) {
return [];
}

// Skip attaching authUser when the job does not use WasAuthenticated Trait
if (! in_array(WasAuthenticated::class, class_uses_recursive($payload['displayName']))) {
return [];
Expand Down
6 changes: 6 additions & 0 deletions src/Middlewares/RestoreAuthenticatedContextMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DatPM\LaravelAuthQueue\Middlewares;

use Illuminate\Queue\Jobs\SyncJob;
use Illuminate\Support\Facades\Auth;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Auth\Authenticatable;
Expand All @@ -12,6 +13,11 @@ class RestoreAuthenticatedContextMiddleware

public function handle($command, callable $next)
{
// If the job is being handled in sync, skip restore logic
if ($command->job instanceof SyncJob) {
return $next($command);
}

Auth::shouldUse('kernel');

$guard = auth();
Expand Down