PHPackages                             mrpunyapal/laravel-auth-jobs - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. mrpunyapal/laravel-auth-jobs

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

mrpunyapal/laravel-auth-jobs
============================

This package allows you to access the authenticated user while processing jobs in the queue.

1.4.0(1mo ago)44368↑12.5%1[1 issues](https://github.com/MrPunyapal/laravel-auth-jobs/issues)MITPHPPHP ^8.2||^8.3||^8.4||^8.5CI passing

Since Apr 17Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/MrPunyapal/laravel-auth-jobs)[ Packagist](https://packagist.org/packages/mrpunyapal/laravel-auth-jobs)[ Docs](https://github.com/mrpunyapal/laravel-auth-jobs)[ GitHub Sponsors](https://github.com/MrPunyapal)[ RSS](/packages/mrpunyapal-laravel-auth-jobs/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (8)Dependencies (30)Versions (11)Used By (0)

This package allows you to access the authenticated user while processing jobs in the queue.
============================================================================================

[](#this-package-allows-you-to-access-the-authenticated-user-while-processing-jobs-in-the-queue)

[![Laravel Compatibility](https://camo.githubusercontent.com/6f1e7751686bf4edac5ba6305e71f8d4bf6d3414a313e63a60f30d569efbc47e/68747470733a2f2f62616467652e6c61726176656c2e636c6f75642f62616467652f6d7270756e796170616c2f6c61726176656c2d617574682d6a6f6273)](https://packagist.org/packages/mrpunyapal/laravel-auth-jobs)[![Latest Version on Packagist](https://camo.githubusercontent.com/a763260d354ef7e9266d58009bc0ce90e4c761668b6b6e6e7ecdb35605cdaf18/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d7270756e796170616c2f6c61726176656c2d617574682d6a6f62732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mrpunyapal/laravel-auth-jobs)[![GitHub Tests Action Status](https://camo.githubusercontent.com/a677c47c91df68a104946f5f96c96db7469e5f849e1e3877fc4958e8c203ca74/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d7270756e796170616c2f6c61726176656c2d617574682d6a6f62732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/mrpunyapal/laravel-auth-jobs/actions?query=workflow%3Arun-tests+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/7087cf598ba4619e902b40c6e01104b301516232d94c1d8a5eb75a9973a1811b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d7270756e796170616c2f6c61726176656c2d617574682d6a6f62732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mrpunyapal/laravel-auth-jobs)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require mrpunyapal/laravel-auth-jobs
```

You can publish the config file with:

```
php artisan vendor:publish --tag="auth-jobs-config"
```

This is the contents of the published config file:

```
return [
    // the middleware groups that are dispatching the jobs which need authentication
    'middleware_groups' => [
        'web',
        // 'api',
    ],

    // the class that provides context keys for storing auth data
    // must implement MrPunyapal\LaravelAuthJobs\Contracts\HasContextKeys
    'context_keys' => \MrPunyapal\LaravelAuthJobs\ContextKeys::class,
];
```

Usage
-----

[](#usage)

This package provides two middleware: `AuthenticateJob`, you can add this middleware to your job class. and you can now access `auth()->user()` in your job class.

### Job Class

[](#job-class)

```
use App\Models\Example;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use MrPunyapal\LaravelAuthJobs\Jobs\Middleware\AuthenticateJob;

class ExampleJob implements ShouldQueue
{
    use Queueable;

    /**
     * Get the middleware the job should pass through.
     *
     * @return array
     */
    public function middleware(): array
    {
        return [new AuthenticateJob];
    }

    public function handle()
    {
        // You can now access auth()->user() here
        $user = auth()->user();

        // authorize your actions
        Gate::authorize('view', Example::class);
    }
}
```

Customizing Context Keys
------------------------

[](#customizing-context-keys)

If you need to use custom context keys (e.g., to avoid conflicts with other packages), you can create your own class implementing `HasContextKeys`:

```
