@@ -107,6 +107,7 @@ This section describes how to use the following features to customize the MongoD
107107authentication 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
159269Password Reminders
0 commit comments