-
Notifications
You must be signed in to change notification settings - Fork 1.4k
DOCSP-42794: Laravel Passport #3113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
ae8eb98
93a3d90
fd30e09
be4a2b2
388db35
275a933
bffbc43
d1afb7b
130cce8
0b959f8
dafc058
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Providers; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use MongoDB\Laravel\Passport\AuthCode; | ||
use MongoDB\Laravel\Passport\Client; | ||
use MongoDB\Laravel\Passport\PersonalAccessClient; | ||
use MongoDB\Laravel\Passport\RefreshToken; | ||
use MongoDB\Laravel\Passport\Token; | ||
|
||
class AppServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register any application services. | ||
*/ | ||
public function register(): void | ||
{ | ||
} | ||
|
||
/** | ||
* Bootstrap any application services. | ||
*/ | ||
public function boot(): void | ||
{ | ||
Passport::useAuthCodeModel(AuthCode::class); | ||
Passport::useClientModel(Client::class); | ||
Passport::usePersonalAccessClientModel(PersonalAccessClient::class); | ||
Passport::useRefreshTokenModel(RefreshToken::class); | ||
Passport::useTokenModel(Token::class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -107,6 +107,7 @@ This section describes how to use the following features to customize the MongoD | |||||||||||||
authentication process: | ||||||||||||||
|
||||||||||||||
- :ref:`laravel-user-auth-sanctum` | ||||||||||||||
- :ref:`laravel-user-auth-passport` | ||||||||||||||
- :ref:`laravel-user-auth-reminders` | ||||||||||||||
|
||||||||||||||
.. _laravel-user-auth-sanctum: | ||||||||||||||
|
@@ -154,6 +155,107 @@ in the Laravel Sanctum guide. | |||||||||||||
To learn more about the ``DocumentModel`` trait, see | ||||||||||||||
:ref:`laravel-third-party-model` in the Eloquent Model Class guide. | ||||||||||||||
|
||||||||||||||
.. _laravel-user-auth-passport: | ||||||||||||||
|
||||||||||||||
Laravel Passport | ||||||||||||||
~~~~~~~~~~~~~~~~ | ||||||||||||||
|
||||||||||||||
Laravel Passport is an OAuth 2.0 server implementation that offers | ||||||||||||||
API authentication for Laravel applications. Use Laravel Passport if | ||||||||||||||
your application requires OAuth2 support. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: maybe you can link to a resource describing Oauth and/or passport? |
||||||||||||||
|
||||||||||||||
Install Laravel Passport | ||||||||||||||
```````````````````````` | ||||||||||||||
|
||||||||||||||
To install Laravel Passport and run the database migrations required | ||||||||||||||
to store OAuth2 clients, run the following command from your project root: | ||||||||||||||
|
||||||||||||||
.. code-block:: bash | ||||||||||||||
|
||||||||||||||
php artisan install:api --passport | ||||||||||||||
|
||||||||||||||
Next, navigate to your ``User`` model and add the ``Laravel\Passport\HasApiTokens`` | ||||||||||||||
trait. This trait provides helper methods that allow you to inspect a user's | ||||||||||||||
authentication token and scopes. The following code shows how to add ``Laravel\Passport\HasApiTokens`` | ||||||||||||||
to your ``app\Models\User.php`` file: | ||||||||||||||
|
||||||||||||||
.. code-block:: php | ||||||||||||||
|
||||||||||||||
<?php | ||||||||||||||
|
||||||||||||||
namespace App\Models; | ||||||||||||||
|
||||||||||||||
use MongoDB\Laravel\Auth\User as Authenticatable; | ||||||||||||||
use Laravel\Passport\HasApiTokens; | ||||||||||||||
|
||||||||||||||
class User extends Authenticatable | ||||||||||||||
{ | ||||||||||||||
use HasApiTokens; | ||||||||||||||
... | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
Then, define an ``api`` authentication guard in your ``config\auth.php`` | ||||||||||||||
file and set the ``driver`` option to ``passport``. This instructs your | ||||||||||||||
application to use Laravel Passport's ``TokenGuard`` class to authenticate | ||||||||||||||
API requests. The following example adds the ``api`` authentication guard | ||||||||||||||
to the ``guards`` array: | ||||||||||||||
|
||||||||||||||
.. code-block:: php | ||||||||||||||
:emphasize-lines: 7-10 | ||||||||||||||
|
||||||||||||||
'guards' => [ | ||||||||||||||
'web' => [ | ||||||||||||||
'driver' => 'session', | ||||||||||||||
'provider' => 'users', | ||||||||||||||
], | ||||||||||||||
Comment on lines
+217
to
+220
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this other entry default? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes the web entry is added automatically |
||||||||||||||
|
||||||||||||||
|
||||||||||||||
'api' => [ | ||||||||||||||
'driver' => 'passport', | ||||||||||||||
'provider' => 'users', | ||||||||||||||
], | ||||||||||||||
], | ||||||||||||||
|
||||||||||||||
Use Laravel Passport with Laravel MongoDB | ||||||||||||||
````````````````````````````````````````` | ||||||||||||||
|
||||||||||||||
After installing Laravel Passport, you must enable Passport compatibility with MongoDB by | ||||||||||||||
defining custom {+odm-short+} models that extend the corresponding Passport models. | ||||||||||||||
To extend each Passport model class, include the ``DocumentModel`` trait in the custom models. | ||||||||||||||
You can define the following {+odm-short+} model classes: | ||||||||||||||
|
||||||||||||||
- ``MongoDB\Laravel\Passport\AuthCode``, which extends ``Laravel\Passport\AuthCode`` | ||||||||||||||
- ``MongoDB\Laravel\Passport\Client``, which extends ``Laravel\Passport\Client`` | ||||||||||||||
- ``MongoDB\Laravel\Passport\PersonalAccessClient``, which extends ``Laravel\Passport\PersonalAccessClient`` | ||||||||||||||
- ``MongoDB\Laravel\Passport\RefreshToken``, which extends ``Laravel\Passport\RefreshToken`` | ||||||||||||||
- ``MongoDB\Laravel\Passport\Token``, which extends ``Laravel\Passport\Token`` | ||||||||||||||
|
||||||||||||||
For example, the following code overrides the default ``Laravel\Passport\AuthCode`` | ||||||||||||||
model class by defining a ``MongoDB\Laravel\Passport\AuthCode`` class and including | ||||||||||||||
the ``DocumentModel`` trait: | ||||||||||||||
|
For example, the following code overrides the default ``Laravel\Passport\AuthCode`` | |
model class by defining a ``MongoDB\Laravel\Passport\AuthCode`` class and including | |
the ``DocumentModel`` trait: | |
The following example code extends the default ``Laravel\Passport\AuthCode`` | |
model class when defining a ``MongoDB\Laravel\Passport\AuthCode`` class and includes | |
the ``DocumentModel`` trait: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: does the user need to create models for each of the listed classes? If so, that should be more clear in a note or something similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah each one needs a custom model. I changed the wording of the paragraph below (line 246) to clarify that, lmk if it's clear enough
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: recommend moving the link up to the intro
You can now use Laravel Passport and MongoDB in your application. For more information, see | |
`Laravel Passport <https://laravel.com/docs/{+laravel-docs-version+}/passport>`__ in the | |
Laravel documentation. | |
The, you can use Laravel Passport with MongoDB in your application. |
Uh oh!
There was an error while loading. Please reload this page.