Skip to content

DOCSP-41241: Laravel Sanctum #3050

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

Merged
merged 9 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/includes/auth/PersonalAccessToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Models;

use Laravel\Sanctum\PersonalAccessToken as SanctumToken;
use MongoDB\Laravel\Eloquent\DocumentModel;

class PersonalAccessToken extends SanctumToken
{
use DocumentModel;

protected $connection = 'mongodb';
protected $collection = 'personal_access_tokens';
protected $primaryKey = '_id';
protected $keyType = 'string';
}
64 changes: 60 additions & 4 deletions docs/user-authentication.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ User Authentication
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:depth: 2
:class: singlecol

Overview
Expand Down Expand Up @@ -97,8 +97,64 @@ manage user authentication, as shown in the following code:
:language: php
:dedent:

Enable Password Reminders
-------------------------
Customize User Authentication
-----------------------------

In this section, you can learn how to use the following features to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: before this sentence, add a sentence explaining why someone might want to make a custom model

customize your MongoDB user authentication process:

- :ref:`laravel-user-auth-sanctum`
- :ref:`laravel-user-auth-reminders`

.. _laravel-user-auth-sanctum:

Laravel Sanctum
~~~~~~~~~~~~~~~

You can install the Laravel Sanctum package to provide API tokens to users
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: add a sentence explaining broadly what laravel sanctum is

and authenticate single-page applications. To manage API tokens, Sanctum stores
each token in a database table and authenticates incoming HTTP requests through
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
each token in a database table and authenticates incoming HTTP requests through
each token in a database table and authenticates incoming HTTP requests by using

the ``Authorization`` header. To authenticate single-page applications, Sanctum
uses Laravel's cookie-based authentication services.

Run the following commands from your project root to install Laravel Sanctum,
then publish and run its migration files:

.. code-block:: bash

composer require laravel/sanctum
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
php artisan migrate

.. tip::

To learn more about installing and configuring Laravel Sanctum, see the
`Installation <https://laravel.com/docs/{+laravel-docs-version+}/sanctum#installation>`__
section of the Laravel Sanctum guide.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To learn more about installing and configuring Laravel Sanctum, see the
`Installation <https://laravel.com/docs/{+laravel-docs-version+}/sanctum#installation>`__
section of the Laravel Sanctum guide.
To learn more about installing and configuring Laravel Sanctum, see
`Installation <https://laravel.com/docs/{+laravel-docs-version+}/sanctum#installation>`__
in the Laravel Sanctum guide.


To use Laravel Sanctum with {+odm-short+}, you must modify the ``PersonalAccessToken``
model provided by Sanctum.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: provide a little more explanation of what is required here


Example
```````

The following code extends the ``PersonalAccessToken`` model provided by Sanctum
to enable MongoDB:

.. literalinclude:: /includes/auth/PersonalAccessToken.php
:language: php
:dedent:

After modifying the ``PersonalAccessToken`` model, instruct Sanctum to use the custom
model by calling the ``usePersonalAccessTokenModel()`` method in one of your application's
service providers. To learn more, see the `Overriding Default Models
<https://laravel.com/docs/{+laravel-docs-version+}/sanctum#overriding-default-models>`__
section of the Laravel Sanctum guide.

.. _laravel-user-auth-reminders:

Password Reminders
~~~~~~~~~~~~~~~~~~

To add support for MongoDB-based password reminders, register the following service
provider in your application:
Expand All @@ -111,7 +167,7 @@ This service provider modifies the internal ``DatabaseReminderRepository``
to enable password reminders.

Example
~~~~~~~
```````

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