1
1
.. _laravel-user-authentication:
2
2
3
3
===================
4
- User authentication
4
+ User Authentication
5
5
===================
6
6
7
7
.. facet::
@@ -11,14 +11,113 @@ User authentication
11
11
.. meta::
12
12
:keywords: php framework, odm, code example
13
13
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: 2
18
+ :class: singlecol
19
+
20
+ Overview
21
+ --------
22
+
23
+ In this guide, you can learn how to use the {+odm-long+} to create a ``User``
24
+ model and configure user authentication.
25
+
26
+ User Model
27
+ ----------
28
+
29
+ By default, Laravel includes an ``App\Models\User`` Eloquent model in your ``app/Models``
30
+ directory. You can extend the ``Jenssegers\Mongodb\Auth\User`` class to configure your
31
+ ``User`` model to use the Laravel Auth module, which provides multiple authentication services.
32
+ To extend this class, navigate to your ``app/Models/User.php`` file and replace the
33
+ ``use Illuminate\Foundation\Auth\User as Authenticatable`` use statement with the following code:
34
+
35
+ .. code-block:: php
36
+
37
+ use Jenssegers\Mongodb\Auth\User as Authenticatable;
38
+
39
+ Ensure that your ``User`` class extends ``Authenticatable``, as shown in the following code:
40
+
41
+ .. code-block:: php
42
+
43
+ class User extends Authenticatable
44
+ {
45
+ ...
46
+ }
47
+
48
+ After configuring your ``User`` model, create a corresponding controller. For instructions on
49
+ creating a controller, see the :ref:`laravel-auth-controller` section on this page.
50
+
51
+ Example
52
+ ~~~~~~~
53
+
54
+ The following code demonstrates a ``User.php`` file that extends the ``Jenssegers\Mongodb\Auth\User``
55
+ class:
56
+
57
+ .. literalinclude:: /includes/auth/AuthUser.php
58
+ :language: php
59
+ :dedent:
60
+
61
+ .. _laravel-auth-controller:
62
+
63
+ User Controller
64
+ ---------------
65
+
66
+ To store functions that manage authentication, create an authentication controller for
67
+ your ``User`` model.
68
+
69
+ Run the following command from your project root to create a controller:
70
+
71
+ .. code-block:: php
72
+
73
+ php artisan make:controller <filename>
74
+
75
+ Example
76
+ ~~~~~~~
77
+
78
+ The following command creates a controller file called ``AuthController.php``:
79
+
80
+ .. code-block:: php
81
+
82
+ php artisan make:controller AuthController
83
+
84
+ The ``AuthController.php`` file can store ``login()`` and ``logout()`` functions to
85
+ manage user authentication, as shown in the following code:
86
+
87
+ .. literalinclude:: /includes/auth/AuthController.php
88
+ :language: php
89
+ :dedent:
90
+
91
+ Enable Password Reminders
92
+ -------------------------
93
+
94
+ To add support for MongoDB-based password reminders, register the following service
95
+ provider in your application:
96
+
97
+ .. code-block:: php
98
+
99
+ MongoDB\Laravel\Auth\PasswordResetServiceProvider::class
100
+
101
+ This service provider modifies the internal ``DatabaseReminderRepository``
102
+ to enable password reminders.
103
+
104
+ Example
105
+ ~~~~~~~
106
+
107
+ The following code updates the ``providers.php`` file in the ``bootstrap`` directory
108
+ of a Laravel application to register the ``PasswordResetServiceProvider`` provider:
16
109
17
110
.. code-block:: php
111
+ :emphasize-lines: 5
18
112
19
- MongoDB\Laravel\Auth\PasswordResetServiceProvider::class,
113
+ return [
114
+ App\Providers\AppServiceProvider::class,
115
+ MongoDB\Laravel\MongoDBServiceProvider::class,
116
+ MongoDB\Laravel\Auth\PasswordResetServiceProvider::class
117
+ ];
20
118
21
- This service provider will slightly modify the internal ``DatabaseReminderRepository``
22
- to add support for MongoDB based password reminders.
119
+ Additional Information
120
+ ----------------------
23
121
24
- If you don't use password reminders, you can omit this service provider.
122
+ To learn more about user authentication, see `Authentication <https://laravel.com/docs/{+laravel-docs-version+}/authentication>`__
123
+ in the Laravel documentation.
0 commit comments