Skip to content

Commit b9cb634

Browse files
committed
Adds queueable authentication context
Implements a mechanism to preserve the authenticated user context when dispatching Laravel queued jobs, notifications, or event listeners. This allows seamless access to the authenticated user who originally dispatched the job through Laravel's auth() manager when the job is being handled. Includes: - A trait `WasAuthenticated` to inject the authenticated context. - A middleware to restore the authentication context when the job is handled. - Tests to ensure the authentication context is preserved.
1 parent dfd9b7f commit b9cb634

39 files changed

+959
-631
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github: :vendor_name
1+
github: datpmwork

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
blank_issues_enabled: false
22
contact_links:
33
- name: Ask a question
4-
url: https://github.com/:vendor_slug/:package_name/discussions/new?category=q-a
4+
url: https://github.com/datpmwork/laravel-queueable-auth-context/discussions/new?category=q-a
55
about: Ask the community for help
66
- name: Request a feature
7-
url: https://github.com/:vendor_slug/:package_name/discussions/new?category=ideas
7+
url: https://github.com/datpmwork/laravel-queueable-auth-context/discussions/new?category=ideas
88
about: Share ideas for new features
99
- name: Report a security issue
10-
url: https://github.com/:vendor_slug/:package_name/security/policy
10+
url: https://github.com/datpmwork/laravel-queueable-auth-context/security/policy
1111
about: Learn how to notify us for sensitive bugs

.github/workflows/run-tests.yml

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
1-
name: run-tests
1+
name: Tests
22

3-
on:
4-
push:
5-
paths:
6-
- '**.php'
7-
- '.github/workflows/run-tests.yml'
8-
- 'phpunit.xml.dist'
9-
- 'composer.json'
10-
- 'composer.lock'
11-
12-
concurrency:
13-
group: ${{ github.workflow }}-${{ github.ref }}
14-
cancel-in-progress: true
3+
on: [push, pull_request]
154

165
jobs:
176
test:
18-
runs-on: ${{ matrix.os }}
19-
timeout-minutes: 5
7+
runs-on: ubuntu-latest
208
strategy:
21-
fail-fast: true
9+
fail-fast: false
2210
matrix:
23-
os: [ubuntu-latest, windows-latest]
24-
php: [8.4, 8.3]
25-
laravel: [12.*, 11.*, 10.*]
26-
stability: [prefer-lowest, prefer-stable]
27-
include:
28-
- laravel: 12.*
29-
testbench: 10.*
30-
- laravel: 11.*
31-
testbench: 9.*
11+
php: [8.0, 8.1, 8.2, 8.3]
12+
laravel: [9.*, 10.*, 11.*, 12.*]
13+
stability: [prefer-stable]
14+
exclude:
15+
# Laravel 11 requires PHP 8.2+
3216
- laravel: 10.*
33-
testbench: 8.*
34-
17+
php: 8.0
18+
# Laravel 11 requires PHP 8.2+
19+
- laravel: 11.*
20+
php: 8.0
21+
- laravel: 11.*
22+
php: 8.1
23+
# Laravel 12 requires PHP 8.2+
24+
- laravel: 12.*
25+
php: 8.0
26+
- laravel: 12.*
27+
php: 8.1
3528

36-
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
29+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }}
3730

3831
steps:
3932
- name: Checkout code
@@ -43,21 +36,18 @@ jobs:
4336
uses: shivammathur/setup-php@v2
4437
with:
4538
php-version: ${{ matrix.php }}
46-
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
39+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite
4740
coverage: none
4841

49-
- name: Setup problem matchers
42+
- name: Remove dev packages for PHP 8.0
5043
run: |
51-
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
52-
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
44+
composer remove larastan/larastan --dev --no-update
45+
composer remove pestphp/pest-plugin-arch --dev --no-update
5346
5447
- name: Install dependencies
5548
run: |
56-
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
49+
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
5750
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
5851
59-
- name: List Installed Dependencies
60-
run: composer show -D
61-
6252
- name: Execute tests
63-
run: vendor/bin/pest --ci
53+
run: vendor/bin/pest

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Changelog
22

3-
All notable changes to `:package_name` will be documented in this file.
3+
All notable changes to `laravel-queueable-auth-context` will be documented in this file.

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) :vendor_name <author@domain.com>
3+
Copyright (c) datpmwork <datpm@datpm.work>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,105 @@
1-
# :package_description
2-
3-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug)
4-
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/:vendor_slug/:package_slug/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3Arun-tests+branch%3Amain)
5-
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/:vendor_slug/:package_slug/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
6-
[![Total Downloads](https://img.shields.io/packagist/dt/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug)
7-
<!--delete-->
8-
---
9-
This repo can be used to scaffold a Laravel package. Follow these steps to get started:
10-
11-
1. Press the "Use this template" button at the top of this repo to create a new repo with the contents of this skeleton.
12-
2. Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files.
13-
3. Have fun creating your package.
14-
4. If you need help creating a package, consider picking up our <a href="https://laravelpackage.training">Laravel Package Training</a> video course.
15-
---
16-
<!--/delete-->
17-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
1+
# Preserve the authenticated user context when dispatching Laravel queued jobs.
182

19-
## Support us
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/datpmwork/laravel-auth-queue.svg?style=flat-square)](https://packagist.org/packages/datpmwork/laravel-auth-queue)
4+
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/datpmwork/laravel-auth-queue/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/datpmwork/laravel-auth-queue/actions?query=workflow%3Arun-tests+branch%3Amain)
5+
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/datpmwork/laravel-auth-queue/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/datpmwork/laravel-auth-queue/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
6+
[![Total Downloads](https://img.shields.io/packagist/dt/datpmwork/laravel-auth-queue.svg?style=flat-square)](https://packagist.org/packages/datpmwork/laravel-auth-queue)
207

21-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/:package_name.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/:package_name)
8+
This package preserves the authenticated user context when dispatching Laravel queued jobs, notifications, or event
9+
listeners.
2210

23-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
11+
It allows you to seamlessly access the authenticated user who originally dispatched the job through Laravel's
12+
auth() manager when the job is being handled.
2413

25-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
14+
This is particularly useful when you need to maintain user context across
15+
asynchronous operations.
2616

27-
## Installation
17+
## Requirements
2818

29-
You can install the package via composer:
19+
- PHP ^7.4 | > 8.0
20+
- Laravel 9.x | 10.x | 11.x | 12.x
3021

31-
```bash
32-
composer require :vendor_slug/:package_slug
33-
```
22+
## Support us
3423

35-
You can publish and run the migrations with:
24+
You can support this project via [GitHub Sponsors](https://github.com/sponsors/datpmwork).
3625

37-
```bash
38-
php artisan vendor:publish --tag=":package_slug-migrations"
39-
php artisan migrate
40-
```
26+
## Installation
4127

42-
You can publish the config file with:
28+
You can install the package via composer:
4329

4430
```bash
45-
php artisan vendor:publish --tag=":package_slug-config"
31+
composer require datpmwork/laravel-auth-queue
4632
```
4733

48-
This is the contents of the published config file:
34+
## Usage
4935

36+
Add `WasAuthenticated` trait to any `Job`, `Notification`, `Listener` which need to access `auth` data when the Job was dispatched
37+
38+
### Example Job
5039
```php
51-
return [
52-
];
40+
class SampleJob implements ShouldQueue
41+
{
42+
use Dispatchable, InteractsWithQueue, Queueable, WasAuthenticated;
43+
44+
public function handle()
45+
{
46+
# auth()->user() was the authenticated user who dispatched this job
47+
logger()->info('Auth ID: '. auth()->id());
48+
}
49+
}
5350
```
5451

55-
Optionally, you can publish the views using
56-
57-
```bash
58-
php artisan vendor:publish --tag=":package_slug-views"
52+
### Example Notification
53+
```php
54+
class SampleNotification extends Notification implements ShouldQueue
55+
{
56+
use Queueable, WasAuthenticated;
57+
58+
public function via(): array
59+
{
60+
return ['database'];
61+
}
62+
63+
public function toDatabase(): array
64+
{
65+
# auth()->user() was the authenticated user who triggered this notification
66+
return [auth()->id()];
67+
}
68+
}
5969
```
6070

61-
## Usage
62-
71+
### Example Subscriber
6372
```php
64-
$variable = new VendorName\Skeleton();
65-
echo $variable->echoPhrase('Hello, VendorName!');
73+
class SampleSubscriber implements ShouldQueue
74+
{
75+
use Queueable, WasAuthenticated;
76+
77+
public function subscribe(Dispatcher $dispatcher)
78+
{
79+
$dispatcher->listen('eloquent.updated: ' . User::class, [self::class, 'onUserUpdated']);
80+
}
81+
82+
public function onUserUpdated(User $user)
83+
{
84+
# auth()->user() was the authenticated user who triggered this event
85+
logger()->info('Auth ID: '. auth()->id());
86+
}
87+
}
6688
```
6789

6890
## Testing
6991

7092
```bash
71-
composer test
93+
./vendor/bin/pest
7294
```
7395

7496
## Changelog
7597

7698
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
7799

78-
## Contributing
79-
80-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
81-
82-
## Security Vulnerabilities
83-
84-
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
85-
86100
## Credits
87101

88-
- [:author_name](https://github.com/:author_username)
102+
- [datpmwork](https://github.com/datpmwork)
89103
- [All Contributors](../../contributors)
90104

91105
## License

composer.json

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,44 @@
11
{
2-
"name": ":vendor_slug/:package_slug",
3-
"description": ":package_description",
2+
"name": "datpmwork/laravel-auth-queue",
3+
"description": "Preserve the authenticated user context when dispatching Laravel queued jobs.",
44
"keywords": [
5-
":vendor_name",
5+
"datpmwork",
66
"laravel",
7-
":package_slug"
7+
"laravel-queueable-auth-context"
88
],
9-
"homepage": "https://github.com/:vendor_slug/:package_slug",
9+
"homepage": "https://github.com/datpmwork/laravel-auth-queue",
1010
"license": "MIT",
1111
"authors": [
1212
{
13-
"name": ":author_name",
14-
"email": "author@domain.com",
15-
"role": "Developer"
13+
"name": "datpmwork",
14+
"email": "datpm@datpm.work",
15+
"role": "Owner"
1616
}
1717
],
1818
"require": {
19-
"php": "^8.4",
20-
"spatie/laravel-package-tools": "^1.16",
21-
"illuminate/contracts": "^10.0||^11.0||^12.0"
19+
"php": "^7.4|^8.0",
20+
"illuminate/support": "^9.0|^10.0|^11.0|^12.0"
2221
},
2322
"require-dev": {
24-
"laravel/pint": "^1.14",
25-
"nunomaduro/collision": "^8.1.1||^7.10.0",
26-
"larastan/larastan": "^2.9||^3.0",
27-
"orchestra/testbench": "^10.0.0||^9.0.0||^8.22.0",
28-
"pestphp/pest": "^3.0",
29-
"pestphp/pest-plugin-arch": "^3.0",
30-
"pestphp/pest-plugin-laravel": "^3.0",
31-
"phpstan/extension-installer": "^1.3||^2.0",
32-
"phpstan/phpstan-deprecation-rules": "^1.1||^2.0",
33-
"phpstan/phpstan-phpunit": "^1.3||^2.0",
34-
"spatie/laravel-ray": "^1.35"
23+
"laravel/pint": "^1.0",
24+
"nunomaduro/collision": "^6.0|^7.0|^8.0",
25+
"larastan/larastan": "^1.0|^2.9|^3.1",
26+
"orchestra/testbench": "^7.0|^8.0|^9.0|^10.0",
27+
"pestphp/pest": "^1.0|^2.0|^3.0",
28+
"pestphp/pest-plugin-laravel": "^1.0|^2.0|^3.0",
29+
"phpstan/extension-installer": "^1.3",
30+
"phpstan/phpstan-deprecation-rules": "^1.1|^2.0",
31+
"phpstan/phpstan-phpunit": "^1.3|^2.0",
32+
"ext-json": "*"
3533
},
3634
"autoload": {
3735
"psr-4": {
38-
"VendorName\\Skeleton\\": "src/",
39-
"VendorName\\Skeleton\\Database\\Factories\\": "database/factories/"
36+
"DatPM\\LaravelAuthQueue\\": "src/"
4037
}
4138
},
4239
"autoload-dev": {
4340
"psr-4": {
44-
"VendorName\\Skeleton\\Tests\\": "tests/",
45-
"Workbench\\App\\": "workbench/app/"
41+
"DatPM\\LaravelAuthQueue\\Tests\\": "tests/"
4642
}
4743
},
4844
"scripts": {
@@ -63,13 +59,10 @@
6359
"extra": {
6460
"laravel": {
6561
"providers": [
66-
"VendorName\\Skeleton\\SkeletonServiceProvider"
67-
],
68-
"aliases": {
69-
"Skeleton": "VendorName\\Skeleton\\Facades\\Skeleton"
70-
}
62+
"DatPM\\LaravelAuthQueue\\LaravelAuthQueueServiceProvider"
63+
]
7164
}
7265
},
73-
"minimum-stability": "dev",
66+
"minimum-stability": "stable",
7467
"prefer-stable": true
7568
}

config/queueable-auth-context.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// config for DatPM/LaravelAuthQueue
4+
return [
5+
6+
];

config/skeleton.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)