Skip to content

Commit d98f396

Browse files
committed
DOCSP-38100: User authentication
1 parent ffacc6b commit d98f396

File tree

3 files changed

+162
-7
lines changed

3 files changed

+162
-7
lines changed

docs/includes/auth/AuthController.php

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

docs/includes/auth/AuthUser.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Jenssegers\Mongodb\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

Lines changed: 106 additions & 7 deletions
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,113 @@ 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: 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:
16109

17110
.. code-block:: php
111+
:emphasize-lines: 5
18112

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+
];
20118

21-
This service provider will slightly modify the internal ``DatabaseReminderRepository``
22-
to add support for MongoDB based password reminders.
119+
Additional Information
120+
----------------------
23121

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

Comments
 (0)