11.. _laravel-user-authentication:
22
33===================
4- User authentication
4+ User Authentication
55===================
66
77.. facet::
@@ -11,14 +11,124 @@ User authentication
1111.. meta::
1212 :keywords: php framework, odm, code example
1313
14- If you want to use Laravel's native Auth functionality, register this included
15- service provider:
14+ .. contents:: On this page
15+ :local:
16+ :backlinks: none
17+ :depth: 1
18+ :class: singlecol
19+
20+ Overview
21+ --------
22+
23+ In this guide, you can learn how to authenticate MongoDB users
24+ by using Laravel's native authentication functionality.
25+
26+ Laravel provides a native ``Auth`` module that includes authentication services,
27+ such as guards that define how users are authenticated and providers that define
28+ how users are retrieved. To learn more about these services, see `Authentication
29+ <https://laravel.com/docs/{+laravel-docs-version+}/authentication>`__ in the
30+ Laravel documentation.
31+
32+ Modify the User Model
33+ ---------------------
34+
35+ By default, Laravel generates the ``User`` Eloquent model in your ``App/Models``
36+ directory. To enable authentication for MongoDB users, your ``User`` model
37+ must extend the ``MongoDB\Laravel\Auth\User`` class.
38+
39+ To extend this class, navigate to your ``app/Models/User.php`` file and replace the
40+ ``use Illuminate\Foundation\Auth\User as Authenticatable`` statement with the following
41+ code:
42+
43+ .. code-block:: php
44+
45+ use MongoDB\Laravel\Auth\User as Authenticatable;
46+
47+ Next, ensure that your ``User`` class extends ``Authenticatable``, as shown in the following
48+ code:
49+
50+ .. code-block:: php
51+
52+ class User extends Authenticatable
53+ {
54+ ...
55+ }
56+
57+ After configuring your ``User`` model, create a corresponding controller. To learn how to
58+ create a controller, see the :ref:`laravel-auth-controller` section on this page.
59+
60+ Example
61+ ~~~~~~~
62+
63+ The following code shows a ``User.php`` file that extends the ``MongoDB\Laravel\Auth\User``
64+ class:
65+
66+ .. literalinclude:: /includes/auth/AuthUser.php
67+ :language: php
68+ :dedent:
69+
70+ .. _laravel-auth-controller:
71+
72+ Create the User Controller
73+ --------------------------
74+
75+ To store functions that manage authentication, create an authentication controller for
76+ your ``User`` model.
77+
78+ Run the following command from your project root to create a controller:
79+
80+ .. code-block:: php
81+
82+ php artisan make:controller <filename>
83+
84+ Example
85+ ~~~~~~~
86+
87+ The following command creates a controller file called ``AuthController.php``:
88+
89+ .. code-block:: php
90+
91+ php artisan make:controller AuthController
92+
93+ The ``AuthController.php`` file can store ``login()`` and ``logout()`` functions to
94+ manage user authentication, as shown in the following code:
95+
96+ .. literalinclude:: /includes/auth/AuthController.php
97+ :language: php
98+ :dedent:
99+
100+ Enable Password Reminders
101+ -------------------------
102+
103+ To add support for MongoDB-based password reminders, register the following service
104+ provider in your application:
105+
106+ .. code-block:: php
107+
108+ MongoDB\Laravel\Auth\PasswordResetServiceProvider::class
109+
110+ This service provider modifies the internal ``DatabaseReminderRepository``
111+ to enable password reminders.
112+
113+ Example
114+ ~~~~~~~
115+
116+ The following code updates the ``providers.php`` file in the ``bootstrap`` directory
117+ of a Laravel application to register the ``PasswordResetServiceProvider`` provider:
16118
17119.. code-block:: php
120+ :emphasize-lines: 4
121+
122+ return [
123+ App\Providers\AppServiceProvider::class,
124+ MongoDB\Laravel\MongoDBServiceProvider::class,
125+ MongoDB\Laravel\Auth\PasswordResetServiceProvider::class
126+ ];
18127
19- MongoDB\Laravel\Auth\PasswordResetServiceProvider::class,
128+ Additional Information
129+ ----------------------
20130
21- This service provider will slightly modify the internal ``DatabaseReminderRepository``
22- to add support for MongoDB based password reminders .
131+ To learn more about user authentication, see `Authentication <https://laravel.com/docs/{+laravel-docs-version+}/authentication>`__
132+ in the Laravel documentation .
23133
24- If you don't use password reminders, you can omit this service provider .
134+ To learn more about Eloquent models, see the :ref:`laravel-eloquent-model-class` guide .
0 commit comments