Skip to content

Commit 994e956

Browse files
norareidyrustagir
andauthored
DOCSP-41241: Laravel Sanctum (#3050)
* DOCSP-41241: Laravel Sanctum * apply phpcbf formatting * edits * code edit, change depth * feedback * edits * JM tech review 1 --------- Co-authored-by: norareidy <[email protected]> Co-authored-by: rustagir <[email protected]>
1 parent ebd2847 commit 994e956

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed
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: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ User Authentication
1414
.. contents:: On this page
1515
:local:
1616
:backlinks: none
17-
:depth: 1
17+
:depth: 2
1818
:class: singlecol
1919

2020
Overview
@@ -97,8 +97,62 @@ 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+
You can customize your authentication files to align with your application's
104+
needs and enable additional authentication features.
105+
106+
This section describes how to use the following features to customize the MongoDB user
107+
authentication process:
108+
109+
- :ref:`laravel-user-auth-sanctum`
110+
- :ref:`laravel-user-auth-reminders`
111+
112+
.. _laravel-user-auth-sanctum:
113+
114+
Laravel Sanctum
115+
~~~~~~~~~~~~~~~
116+
117+
Laravel Sanctum is an authentication package that can manage API requests and
118+
single-page application authentication. To manage API requests, Sanctum issues
119+
API tokens that are stored in the database and authenticates incoming HTTP
120+
requests by using the ``Authorization`` header. To authenticate single-page applications,
121+
Sanctum uses Laravel's cookie-based authentication services.
122+
123+
You can install Laravel Sanctum to manage your application's authentication
124+
process. Run the following commands from your project root to install Laravel
125+
Sanctum and publish its migration file:
126+
127+
.. code-block:: bash
128+
129+
composer require laravel/sanctum
130+
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
131+
132+
To use Laravel Sanctum with {+odm-short+}, modify the ``PersonalAccessToken`` model provided
133+
by Sanctum to use the ``DocumentModel`` trait from the ``MongoDB\Laravel\Eloquent`` namespace.
134+
The following code modifies the ``PersonalAccessToken`` model to enable MongoDB:
135+
136+
.. literalinclude:: /includes/auth/PersonalAccessToken.php
137+
:language: php
138+
:dedent:
139+
140+
Next, run the following command to modify the database schema:
141+
142+
.. code-block:: bash
143+
144+
php artisan migrate
145+
146+
You can now instruct Sanctum to use the custom ``PersonalAccessToken`` model by calling
147+
the ``usePersonalAccessTokenModel()`` method in one of your application's
148+
service providers. To learn more, see `Overriding Default Models
149+
<https://laravel.com/docs/{+laravel-docs-version+}/sanctum#overriding-default-models>`__
150+
in the Laravel Sanctum guide.
151+
152+
.. _laravel-user-auth-reminders:
153+
154+
Password Reminders
155+
~~~~~~~~~~~~~~~~~~
102156

103157
To add support for MongoDB-based password reminders, register the following service
104158
provider in your application:
@@ -111,7 +165,7 @@ This service provider modifies the internal ``DatabaseReminderRepository``
111165
to enable password reminders.
112166

113167
Example
114-
~~~~~~~
168+
```````
115169

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

0 commit comments

Comments
 (0)