Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 57e8910

Browse filesBrowse files
Merge 4.7 into 4.8 (#3127)
2 parents 6a8a2e3 + 24abdfa commit 57e8910
Copy full SHA for 57e8910

File tree

Expand file treeCollapse file tree

2 files changed

+142
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+142
-0
lines changed
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use MongoDB\Laravel\Passport\AuthCode;
7+
use MongoDB\Laravel\Passport\Client;
8+
use MongoDB\Laravel\Passport\PersonalAccessClient;
9+
use MongoDB\Laravel\Passport\RefreshToken;
10+
use MongoDB\Laravel\Passport\Token;
11+
12+
class AppServiceProvider extends ServiceProvider
13+
{
14+
/**
15+
* Register any application services.
16+
*/
17+
public function register(): void
18+
{
19+
}
20+
21+
/**
22+
* Bootstrap any application services.
23+
*/
24+
public function boot(): void
25+
{
26+
Passport::useAuthCodeModel(AuthCode::class);
27+
Passport::useClientModel(Client::class);
28+
Passport::usePersonalAccessClientModel(PersonalAccessClient::class);
29+
Passport::useRefreshTokenModel(RefreshToken::class);
30+
Passport::useTokenModel(Token::class);
31+
}
32+
}

‎docs/user-authentication.txt

Copy file name to clipboardExpand all lines: docs/user-authentication.txt
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ This section describes how to use the following features to customize the MongoD
107107
authentication process:
108108

109109
- :ref:`laravel-user-auth-sanctum`
110+
- :ref:`laravel-user-auth-passport`
110111
- :ref:`laravel-user-auth-reminders`
111112

112113
.. _laravel-user-auth-sanctum:
@@ -154,6 +155,115 @@ in the Laravel Sanctum guide.
154155
To learn more about the ``DocumentModel`` trait, see
155156
:ref:`laravel-third-party-model` in the Eloquent Model Class guide.
156157

158+
.. _laravel-user-auth-passport:
159+
160+
Laravel Passport
161+
~~~~~~~~~~~~~~~~
162+
163+
Laravel Passport is an OAuth 2.0 server implementation that offers
164+
API authentication for Laravel applications. Use Laravel Passport if
165+
your application requires OAuth2 support.
166+
167+
.. tip::
168+
169+
To learn more about Laravel Passport and the OAuth 2.0 protocol, see
170+
the following resources:
171+
172+
- `Laravel Passport <https://laravel.com/docs/{+laravel-docs-version+}/passport>`__ in the
173+
Laravel documentation.
174+
175+
- `OAuth 2.0 <https://oauth.net/2/>`__ on the OAuth website.
176+
177+
Install Laravel Passport
178+
````````````````````````
179+
180+
To install Laravel Passport and run the database migrations required
181+
to store OAuth2 clients, run the following command from your project root:
182+
183+
.. code-block:: bash
184+
185+
php artisan install:api --passport
186+
187+
Next, navigate to your ``User`` model and add the ``Laravel\Passport\HasApiTokens``
188+
trait. This trait provides helper methods that allow you to inspect a user's
189+
authentication token and scopes. The following code shows how to add ``Laravel\Passport\HasApiTokens``
190+
to your ``app\Models\User.php`` file:
191+
192+
.. code-block:: php
193+
194+
<?php
195+
196+
namespace App\Models;
197+
198+
use MongoDB\Laravel\Auth\User as Authenticatable;
199+
use Laravel\Passport\HasApiTokens;
200+
201+
class User extends Authenticatable
202+
{
203+
use HasApiTokens;
204+
...
205+
}
206+
207+
Then, define an ``api`` authentication guard in your ``config\auth.php``
208+
file and set the ``driver`` option to ``passport``. This instructs your
209+
application to use Laravel Passport's ``TokenGuard`` class to authenticate
210+
API requests. The following example adds the ``api`` authentication guard
211+
to the ``guards`` array:
212+
213+
.. code-block:: php
214+
:emphasize-lines: 6-9
215+
216+
'guards' => [
217+
'web' => [
218+
'driver' => 'session',
219+
'provider' => 'users',
220+
],
221+
'api' => [
222+
'driver' => 'passport',
223+
'provider' => 'users',
224+
],
225+
],
226+
227+
Use Laravel Passport with Laravel MongoDB
228+
`````````````````````````````````````````
229+
230+
After installing Laravel Passport, you must enable Passport compatibility with MongoDB by
231+
defining custom {+odm-short+} models that extend the corresponding Passport models.
232+
To extend each Passport model class, include the ``DocumentModel`` trait in the custom models.
233+
You can define the following {+odm-short+} model classes:
234+
235+
- ``MongoDB\Laravel\Passport\AuthCode``, which extends ``Laravel\Passport\AuthCode``
236+
- ``MongoDB\Laravel\Passport\Client``, which extends ``Laravel\Passport\Client``
237+
- ``MongoDB\Laravel\Passport\PersonalAccessClient``, which extends ``Laravel\Passport\PersonalAccessClient``
238+
- ``MongoDB\Laravel\Passport\RefreshToken``, which extends ``Laravel\Passport\RefreshToken``
239+
- ``MongoDB\Laravel\Passport\Token``, which extends ``Laravel\Passport\Token``
240+
241+
The following example code extends the default ``Laravel\Passport\AuthCode``
242+
model class when defining a ``MongoDB\Laravel\Passport\AuthCode`` class and includes
243+
the ``DocumentModel`` trait:
244+
245+
.. code-block:: php
246+
247+
class MongoDB\Laravel\Passport\AuthCode extends Laravel\Passport\AuthCode
248+
{
249+
use MongoDB\Laravel\Eloquent\DocumentModel;
250+
251+
protected $primaryKey = '_id';
252+
protected $keyType = 'string';
253+
}
254+
255+
After defining custom models that extend each ``Laravel\Passport`` class, instruct
256+
Passport to use the models in the ``boot()`` method of your application's
257+
``App\Providers\AppServiceProvider`` class. The following example adds each custom
258+
model to the ``boot()`` method:
259+
260+
.. literalinclude:: /includes/auth/AppServiceProvider.php
261+
:language: php
262+
:emphasize-lines: 26-30
263+
:dedent:
264+
265+
Then, you can use Laravel Passport and MongoDB in your application.
266+
157267
.. _laravel-user-auth-reminders:
158268

159269
Password Reminders

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.