Skip to content

Commit 015943a

Browse files
Merge 4.5 into 4.6 (#3046)
2 parents 6cebcec + 7df42cd commit 015943a

File tree

3 files changed

+177
-7
lines changed

3 files changed

+177
-7
lines changed

docs/includes/auth/AuthController.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Auth;
7+
use Illuminate\Validation\ValidationException;
8+
9+
use function response;
10+
11+
class AuthController extends Controller
12+
{
13+
public function login(Request $request)
14+
{
15+
$request->validate([
16+
'email' => 'required|email',
17+
'password' => 'required',
18+
]);
19+
20+
if (Auth::attempt($request->only('email', 'password'))) {
21+
return response()->json([
22+
'user' => Auth::user(),
23+
'message' => 'Successfully logged in',
24+
]);
25+
}
26+
27+
throw ValidationException::withMessages([
28+
'email' => ['The provided credentials are incorrect.'],
29+
]);
30+
}
31+
32+
public function logout()
33+
{
34+
Auth::logout();
35+
36+
return response()->json(['message' => 'Successfully logged out']);
37+
}
38+
}

docs/includes/auth/AuthUser.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use MongoDB\Laravel\Auth\User as Authenticatable;
6+
7+
class User extends Authenticatable
8+
{
9+
protected $connection = 'mongodb';
10+
protected $collection = 'users';
11+
12+
protected $fillable = [
13+
'name',
14+
'email',
15+
'password',
16+
];
17+
18+
protected $hidden = [
19+
'password',
20+
'remember_token',
21+
];
22+
}

docs/user-authentication.txt

+117-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

Comments
 (0)