Skip to content

Commit 9c15cd0

Browse files
committed
DOCSP-41241: Laravel Sanctum
1 parent 015943a commit 9c15cd0

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

docs/includes/auth/Sanctum.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Laravel\Sanctum\PersonalAccessToken as SanctumToken;
6+
use MongoDB\Laravel\Eloquent\DocumentModel;
7+
8+
class PersonalAccessToken extends SanctumToken
9+
{
10+
use DocumentModel;
11+
12+
protected $connection = 'mongodb';
13+
protected $collection = 'personal_access_tokens';
14+
protected $primaryKey = '_id';
15+
protected $keyType = 'string';
16+
}

docs/user-authentication.txt

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,58 @@ manage user authentication, as shown in the following code:
9797
:language: php
9898
:dedent:
9999

100-
Enable Password Reminders
101-
-------------------------
100+
Customize User Authentication
101+
-----------------------------
102+
103+
In this section, you can learn how to use the following features to
104+
customize your MongoDB user authentication process:
105+
106+
- :ref:`laravel-user-auth-sanctum`
107+
- :ref:`laravel-user-auth-reminders`
108+
109+
.. _laravel-user-auth-sanctum:
110+
111+
Laravel Sanctum
112+
~~~~~~~~~~~~~~~
113+
114+
You can install the Laravel Sanctum package to provide API tokens to users
115+
and authenticate single-page applications. To manage API tokens, Sanctum stores
116+
each token in a database table and authenticates incoming HTTP requests through
117+
the ``Authorization`` header. To authenticate single-page applications, Sanctum
118+
uses Laravel's cookie-based authentication services.
119+
120+
Run the following commands from your project root to install Laravel Sanctum,
121+
then publish and run its migration files:
122+
123+
.. code-block:: php
124+
125+
composer require laravel/sanctum
126+
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
127+
php artisan migrate
128+
129+
To use Laravel Sanctum with {+odm-short+}, you must modify the ``PersonalAccessToken``
130+
model provided by Sanctum.
131+
132+
Example
133+
```````
134+
135+
The following code extends the ``PersonalAccessToken`` model provided by Sanctum
136+
to enable MongoDB:
137+
138+
.. literalinclude:: /includes/auth/PersonalAccessToken.php
139+
:language: php
140+
:dedent:
141+
142+
After modifying the ``PersonalAccessToken`` model, instruct Sanctum to use the custom
143+
model by calling the ``usePersonalAccessTokenModel()`` method in one of your application's
144+
service providers. To learn more, see the `Overriding Default Models
145+
<https://laravel.com/docs/{+laravel-docs-version+}/sanctum#overriding-default-models>`__
146+
section of the Sanctum documentation.
147+
148+
.. _laravel-user-auth-reminders:
149+
150+
Password Reminders
151+
~~~~~~~~~~~~~~~~~~
102152

103153
To add support for MongoDB-based password reminders, register the following service
104154
provider in your application:
@@ -111,7 +161,7 @@ This service provider modifies the internal ``DatabaseReminderRepository``
111161
to enable password reminders.
112162

113163
Example
114-
~~~~~~~
164+
```````
115165

116166
The following code updates the ``providers.php`` file in the ``bootstrap`` directory
117167
of a Laravel application to register the ``PasswordResetServiceProvider`` provider:

0 commit comments

Comments
 (0)