|
1 |
| -# :package_description |
2 |
| - |
3 |
| -[](https://packagist.org/packages/:vendor_slug/:package_slug) |
4 |
| -[](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3Arun-tests+branch%3Amain) |
5 |
| -[](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) |
6 |
| -[](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. |
18 | 2 |
|
19 |
| -## Support us |
| 3 | +[](https://packagist.org/packages/datpmwork/laravel-auth-queue) |
| 4 | +[](https://github.com/datpmwork/laravel-auth-queue/actions?query=workflow%3Arun-tests+branch%3Amain) |
| 5 | +[](https://github.com/datpmwork/laravel-auth-queue/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) |
| 6 | +[](https://packagist.org/packages/datpmwork/laravel-auth-queue) |
20 | 7 |
|
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. |
22 | 10 |
|
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. |
24 | 13 |
|
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. |
26 | 16 |
|
27 |
| -## Installation |
| 17 | +## Requirements |
28 | 18 |
|
29 |
| -You can install the package via composer: |
| 19 | +- PHP ^7.4 | > 8.0 |
| 20 | +- Laravel 9.x | 10.x | 11.x | 12.x |
30 | 21 |
|
31 |
| -```bash |
32 |
| -composer require :vendor_slug/:package_slug |
33 |
| -``` |
| 22 | +## Support us |
34 | 23 |
|
35 |
| -You can publish and run the migrations with: |
| 24 | +You can support this project via [GitHub Sponsors](https://github.com/sponsors/datpmwork). |
36 | 25 |
|
37 |
| -```bash |
38 |
| -php artisan vendor:publish --tag=":package_slug-migrations" |
39 |
| -php artisan migrate |
40 |
| -``` |
| 26 | +## Installation |
41 | 27 |
|
42 |
| -You can publish the config file with: |
| 28 | +You can install the package via composer: |
43 | 29 |
|
44 | 30 | ```bash
|
45 |
| -php artisan vendor:publish --tag=":package_slug-config" |
| 31 | +composer require datpmwork/laravel-auth-queue |
46 | 32 | ```
|
47 | 33 |
|
48 |
| -This is the contents of the published config file: |
| 34 | +## Usage |
49 | 35 |
|
| 36 | +Add `WasAuthenticated` trait to any `Job`, `Notification`, `Listener` which need to access `auth` data when the Job was dispatched |
| 37 | + |
| 38 | +### Example Job |
50 | 39 | ```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 | +} |
53 | 50 | ```
|
54 | 51 |
|
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 | +} |
59 | 69 | ```
|
60 | 70 |
|
61 |
| -## Usage |
62 |
| - |
| 71 | +### Example Subscriber |
63 | 72 | ```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 | +} |
66 | 88 | ```
|
67 | 89 |
|
68 | 90 | ## Testing
|
69 | 91 |
|
70 | 92 | ```bash
|
71 |
| -composer test |
| 93 | +./vendor/bin/pest |
72 | 94 | ```
|
73 | 95 |
|
74 | 96 | ## Changelog
|
75 | 97 |
|
76 | 98 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
|
77 | 99 |
|
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 |
| - |
86 | 100 | ## Credits
|
87 | 101 |
|
88 |
| -- [:author_name](https://github.com/:author_username) |
| 102 | +- [datpmwork](https://github.com/datpmwork) |
89 | 103 | - [All Contributors](../../contributors)
|
90 | 104 |
|
91 | 105 | ## License
|
|
0 commit comments