|
2 | 2 | title: Queues
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -If you want to use `MsGraph` within a job or command, you can use the `login` method to authenticate the user. Make sure to pass the user model to the `login` method. The User needs to have the `access_token` and `refresh_token` stored in the database. |
| 5 | +When using `MsGraph` within jobs, commands, or other queued processes, you need to authenticate the user explicitly. Ensure the user model has `access_token` and `refresh_token` stored in the database. Use the `login` method to authenticate the user before making any API calls: |
6 | 6 |
|
7 | 7 | ```php
|
8 |
| -MsGraph::login(User:find(1)); |
9 |
| - |
10 |
| -MsGraph->get('me'); |
| 8 | +MsGraph::login(User::find(1)); |
| 9 | +MsGraph::get('me'); |
11 | 10 | ```
|
12 | 11 |
|
13 |
| -Example Job: |
| 12 | +Here's an example of how to structure a job that uses `MsGraph`: |
14 | 13 |
|
15 | 14 | ```php
|
16 | 15 | <?php
|
17 | 16 |
|
18 | 17 | namespace App\Jobs;
|
19 | 18 |
|
20 | 19 | use App\Models\User;
|
21 |
| -// ... |
22 |
| - |
23 |
| -class ExampleJob implements ShouldQueue |
| 20 | +use Dcblogdev\MsGraph\Facades\MsGraph; |
| 21 | +use Illuminate\Bus\Queueable; |
| 22 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 23 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 24 | +use Illuminate\Queue\InteractsWithQueue; |
| 25 | +use Illuminate\Queue\SerializesModels; |
| 26 | + |
| 27 | +class ExampleMsGraphJob implements ShouldQueue |
24 | 28 | {
|
25 | 29 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
26 | 30 |
|
27 |
| - public $user; |
| 31 | + protected $user; |
28 | 32 |
|
29 |
| - /** |
30 |
| - * Create a new job instance. |
31 |
| - */ |
32 | 33 | public function __construct(User $user)
|
33 | 34 | {
|
34 | 35 | $this->user = $user;
|
35 | 36 | }
|
36 | 37 |
|
37 |
| - /** |
38 |
| - * Execute the job. |
39 |
| - */ |
40 | 38 | public function handle(): void
|
41 | 39 | {
|
42 | 40 | MsGraph::login($this->user);
|
43 |
| - |
44 |
| - $me = MsGraph::get("me"); |
| 41 | + $userData = MsGraph::get('me'); |
| 42 | + // Process $userData as needed |
45 | 43 | }
|
46 | 44 | }
|
47 |
| -``` |
| 45 | +``` |
| 46 | + |
| 47 | +Dispatch this job with a user instance: |
| 48 | + |
| 49 | +```php |
| 50 | +ExampleMsGraphJob::dispatch($user); |
| 51 | +``` |
| 52 | + |
| 53 | +This approach ensures that the Microsoft Graph API calls are made with the correct user context, even in background processes. |
0 commit comments