@@ -161,44 +161,78 @@ Laravel Passport
161
161
~~~~~~~~~~~~~~~~
162
162
163
163
Laravel Passport is an OAuth 2.0 server implementation that offers
164
- API authentication for Laravel applications. The Laravel MongoDB Passport
165
- service provider enables Laravel Passport support for applications that
166
- use MongoDB.
164
+ API authentication for Laravel applications. Use Laravel Passport if
165
+ your application requires OAuth2 support.
167
166
168
- To install Laravel MongoDB Passport, run the following command from
169
- your project root:
167
+ To install Laravel Passport and run the database migrations required
168
+ to store OAuth2 clients, run the following command from your project root:
170
169
171
170
.. code-block:: bash
172
171
173
- composer require designmynight/laravel-mongodb -passport
172
+ php artisan install:api - -passport
174
173
175
- Next, navigate to your application's ``config/app.php`` file and
176
- add the following service provider to the ``providers`` array:
177
-
178
- .. code-block:: php
179
-
180
- DesignMyNight\Mongodb\MongodbPassportServiceProvider::class,
181
-
182
- Then, ensure your ``User`` class extends ``DesignMyNight\Mongodb\Auth\User.php``
183
- instead of the default ``Illuminate\Foundation\Auth\User``. The following code
184
- shows how to modify your ``app\Models\User.php`` file to extend ``DesignMyNight\Mongodb\Auth\User.php``:
174
+ Next, navigate to your ``User`` model and add the ``Laravel\Passport\HasApiTokens``
175
+ trait. This trait provides helper methods that allow you to inspect a user's
176
+ authentication token and scopes. The following code shows how to add ``Laravel\Passport\HasApiTokens``
177
+ to your ``app\Models\User.php`` file:
185
178
186
179
.. code-block:: php
187
180
188
181
<?php
189
182
190
183
namespace App\Models;
191
184
192
- use Illuminate\Notifications\Notifiable ;
193
- use DesignMyNight\Mongodb\Auth\User as Authenticatable ;
185
+ use MongoDB\Laravel\Auth\User as Authenticatable ;
186
+ use Laravel\Passport\HasApiTokens ;
194
187
195
188
class User extends Authenticatable
196
189
{
197
- use Notifiable ;
190
+ use HasApiTokens ;
198
191
...
199
192
}
200
193
201
- You can now use Laravel Passport in your application. For more information, see
194
+ Then, define an ``api`` authentication guard in your ``config\auth.php``
195
+ file and set the ``driver`` option to ``passport``. This instructs your
196
+ application to use Laravel Passport's ``TokenGuard`` class to authenticate
197
+ API requests. The following example adds the ``api`` authentication guard
198
+ to the ``guards`` array:
199
+
200
+ .. code-block:: php
201
+ :emphasize-lines: 7-10
202
+
203
+ 'guards' => [
204
+ 'web' => [
205
+ 'driver' => 'session',
206
+ 'provider' => 'users',
207
+ ],
208
+
209
+ 'api' => [
210
+ 'driver' => 'passport',
211
+ 'provider' => 'users',
212
+ ],
213
+ ],
214
+
215
+ Lastly, you must override the default Laravel Passport models by defining a custom model
216
+ that extends the corresponding Laravel Passport model. by including the ``DocumentModel`` trait. This trait allows you
217
+ to make Laravel Passport compatible with MongoDB. The following code defines
218
+ a series of model classes that extend the corresponding ``Laravel\Passport`` models:
219
+
220
+ .. code-block:: php
221
+
222
+ class MongoDB\Laravel\Passport\AuthCode extends Laravel\Passport\AuthCode
223
+ {
224
+ use MongoDB\Laravel\Eloquent\DocumentModel;
225
+
226
+ protected $primaryKey = '_id';
227
+ protected $keyType = 'string';
228
+ }
229
+
230
+ class MongoDB\Laravel\Passport\Client extends Laravel\Passport\Client;
231
+ class MongoDB\Laravel\Passport\PersonalAccessClient extends Laravel\Passport\PersonalAccessClient;
232
+ class MongoDB\Laravel\Passport\RefreshToken extends Laravel\Passport\RefreshToken;
233
+ class MongoDB\Laravel\Passport\Token extends Laravel\Passport\Token;
234
+
235
+ You can now use Laravel Passport and MongoDB in your application. For more information, see
202
236
`Laravel Passport <https://laravel.com/docs/{+laravel-docs-version+}/passport>`__ in the
203
237
Laravel documentation.
204
238
0 commit comments