From d98f39690813a905079d38edfc2b3b038811c15d Mon Sep 17 00:00:00 2001 From: norareidy Date: Mon, 8 Jul 2024 11:47:35 -0400 Subject: [PATCH 1/7] DOCSP-38100: User authentication --- docs/includes/auth/AuthController.php | 34 ++++++++ docs/includes/auth/AuthUser.php | 22 +++++ docs/user-authentication.txt | 113 ++++++++++++++++++++++++-- 3 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 docs/includes/auth/AuthController.php create mode 100644 docs/includes/auth/AuthUser.php diff --git a/docs/includes/auth/AuthController.php b/docs/includes/auth/AuthController.php new file mode 100644 index 000000000..1168483c9 --- /dev/null +++ b/docs/includes/auth/AuthController.php @@ -0,0 +1,34 @@ +namespace App\Http\Controllers; + +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Auth; +use Illuminate\Validation\ValidationException; + +class AuthController extends Controller +{ + public function login(Request $request) + { + $request->validate([ + 'email' => 'required|email', + 'password' => 'required', + ]); + + if (Auth::attempt($request->only('email', 'password'))) { + return response()->json([ + 'user' => Auth::user(), + 'message' => 'Successfully logged in', + ]); + } + + throw ValidationException::withMessages([ + 'email' => ['The provided credentials are incorrect.'], + ]); + } + + public function logout() + { + Auth::logout(); + + return response()->json(['message' => 'Successfully logged out']); + } +} \ No newline at end of file diff --git a/docs/includes/auth/AuthUser.php b/docs/includes/auth/AuthUser.php new file mode 100644 index 000000000..6d55abc7e --- /dev/null +++ b/docs/includes/auth/AuthUser.php @@ -0,0 +1,22 @@ + + +Example +~~~~~~~ + +The following command creates a controller file called ``AuthController.php``: + +.. code-block:: php + + php artisan make:controller AuthController + +The ``AuthController.php`` file can store ``login()`` and ``logout()`` functions to +manage user authentication, as shown in the following code: + +.. literalinclude:: /includes/auth/AuthController.php + :language: php + :dedent: + +Enable Password Reminders +------------------------- + +To add support for MongoDB-based password reminders, register the following service +provider in your application: + +.. code-block:: php + + MongoDB\Laravel\Auth\PasswordResetServiceProvider::class + +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: .. code-block:: php + :emphasize-lines: 5 - MongoDB\Laravel\Auth\PasswordResetServiceProvider::class, + return [ + App\Providers\AppServiceProvider::class, + MongoDB\Laravel\MongoDBServiceProvider::class, + MongoDB\Laravel\Auth\PasswordResetServiceProvider::class + ]; -This service provider will slightly modify the internal ``DatabaseReminderRepository`` -to add support for MongoDB based password reminders. +Additional Information +---------------------- -If you don't use password reminders, you can omit this service provider. +To learn more about user authentication, see `Authentication `__ +in the Laravel documentation. \ No newline at end of file From b09e4c2a7f0b4a2509ef2d568230b13b7042dd79 Mon Sep 17 00:00:00 2001 From: norareidy Date: Mon, 8 Jul 2024 12:06:46 -0400 Subject: [PATCH 2/7] add info --- docs/user-authentication.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index 3b2138fab..858a42f62 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -14,7 +14,7 @@ User Authentication .. contents:: On this page :local: :backlinks: none - :depth: 2 + :depth: 1 :class: singlecol Overview @@ -108,7 +108,7 @@ The following code updates the ``providers.php`` file in the ``bootstrap`` direc of a Laravel application to register the ``PasswordResetServiceProvider`` provider: .. code-block:: php - :emphasize-lines: 5 + :emphasize-lines: 4 return [ App\Providers\AppServiceProvider::class, @@ -120,4 +120,6 @@ Additional Information ---------------------- To learn more about user authentication, see `Authentication `__ -in the Laravel documentation. \ No newline at end of file +in the Laravel documentation. + +To learn more about Eloquent models, see the :ref:`laravel-eloquent-model-class` guide. \ No newline at end of file From a7e7b20f23af7589de7a1d622bef92c7856a34ca Mon Sep 17 00:00:00 2001 From: norareidy Date: Mon, 8 Jul 2024 13:11:21 -0400 Subject: [PATCH 3/7] wording --- docs/user-authentication.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index 858a42f62..1b247a34e 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -20,7 +20,7 @@ User Authentication Overview -------- -In this guide, you can learn how to use the {+odm-long+} to create a ``User`` +In this guide, you can learn how to use the {+odm-long+} to modify the ``User`` model and configure user authentication. User Model From f1f0df269fe9e6a969a264444c4961d218a0677e Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 9 Jul 2024 10:34:25 -0400 Subject: [PATCH 4/7] tech review + more info --- docs/includes/auth/AuthController.php | 2 ++ docs/includes/auth/AuthUser.php | 2 +- docs/user-authentication.txt | 24 ++++++++++++++++-------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/docs/includes/auth/AuthController.php b/docs/includes/auth/AuthController.php index 1168483c9..0607a6b77 100644 --- a/docs/includes/auth/AuthController.php +++ b/docs/includes/auth/AuthController.php @@ -1,3 +1,5 @@ +`__ in the +Laravel documentation. User Model ---------- -By default, Laravel includes an ``App\Models\User`` Eloquent model in your ``app/Models`` -directory. You can extend the ``Jenssegers\Mongodb\Auth\User`` class to configure your -``User`` model to use the Laravel Auth module, which provides multiple authentication services. +By default, Laravel generates the ``User`` Eloquent model in your ``App/Models`` +directory. To enable authentication for users stored in MongoDB, your ``User`` model +must extend the ``MongoDB\Laravel\Auth\User`` class. + To extend this class, navigate to your ``app/Models/User.php`` file and replace the -``use Illuminate\Foundation\Auth\User as Authenticatable`` use statement with the following code: +``use Illuminate\Foundation\Auth\User as Authenticatable`` use statement with the following +code: .. code-block:: php - use Jenssegers\Mongodb\Auth\User as Authenticatable; + use MongoDB\Laravel\Auth\User as Authenticatable; Ensure that your ``User`` class extends ``Authenticatable``, as shown in the following code: @@ -51,7 +59,7 @@ creating a controller, see the :ref:`laravel-auth-controller` section on this pa Example ~~~~~~~ -The following code demonstrates a ``User.php`` file that extends the ``Jenssegers\Mongodb\Auth\User`` +The following code demonstrates a ``User.php`` file that extends the ``MongoDB\Laravel\Auth\User`` class: .. literalinclude:: /includes/auth/AuthUser.php From 85db22ab16e363abc5235de85bd2f89efb7ee44d Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 9 Jul 2024 14:35:17 +0000 Subject: [PATCH 5/7] apply phpcbf formatting --- docs/includes/auth/AuthController.php | 36 ++++++++++++++------------- docs/includes/auth/AuthUser.php | 2 +- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/docs/includes/auth/AuthController.php b/docs/includes/auth/AuthController.php index 0607a6b77..c76552cbe 100644 --- a/docs/includes/auth/AuthController.php +++ b/docs/includes/auth/AuthController.php @@ -6,31 +6,33 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Validation\ValidationException; +use function response; + class AuthController extends Controller { - public function login(Request $request) - { - $request->validate([ + public function login(Request $request) + { + $request->validate([ 'email' => 'required|email', 'password' => 'required', - ]); + ]); - if (Auth::attempt($request->only('email', 'password'))) { + if (Auth::attempt($request->only('email', 'password'))) { return response()->json([ - 'user' => Auth::user(), - 'message' => 'Successfully logged in', + 'user' => Auth::user(), + 'message' => 'Successfully logged in', ]); - } + } - throw ValidationException::withMessages([ + throw ValidationException::withMessages([ 'email' => ['The provided credentials are incorrect.'], - ]); - } + ]); + } - public function logout() - { - Auth::logout(); + public function logout() + { + Auth::logout(); - return response()->json(['message' => 'Successfully logged out']); - } -} \ No newline at end of file + return response()->json(['message' => 'Successfully logged out']); + } +} diff --git a/docs/includes/auth/AuthUser.php b/docs/includes/auth/AuthUser.php index a25ac024f..8b6a0f173 100644 --- a/docs/includes/auth/AuthUser.php +++ b/docs/includes/auth/AuthUser.php @@ -19,4 +19,4 @@ class User extends Authenticatable 'password', 'remember_token', ]; -} \ No newline at end of file +} From c03fc05a33287b71482b0116436fcbe0bf274f25 Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 16 Jul 2024 10:08:38 -0400 Subject: [PATCH 6/7] header wording --- docs/user-authentication.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index 6440fee13..5281dbb45 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -29,8 +29,8 @@ how users are retrieved. To learn more about these services, see `Authentication `__ in the Laravel documentation. -User Model ----------- +Modify the User Model +--------------------- By default, Laravel generates the ``User`` Eloquent model in your ``App/Models`` directory. To enable authentication for users stored in MongoDB, your ``User`` model @@ -68,8 +68,8 @@ class: .. _laravel-auth-controller: -User Controller ---------------- +Create the User Controller +-------------------------- To store functions that manage authentication, create an authentication controller for your ``User`` model. From 6dff65430aebdec5e9c98206ab8cd416b52b8b63 Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 16 Jul 2024 11:05:27 -0400 Subject: [PATCH 7/7] MW feedback --- docs/user-authentication.txt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index 5281dbb45..b86a8659f 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -20,10 +20,10 @@ User Authentication Overview -------- -In this guide, you can learn how to authenticate users stored in MongoDB +In this guide, you can learn how to authenticate MongoDB users by using Laravel's native authentication functionality. -Laravel provides a native Auth module that includes authentication services, +Laravel provides a native ``Auth`` module that includes authentication services, such as guards that define how users are authenticated and providers that define how users are retrieved. To learn more about these services, see `Authentication `__ in the @@ -33,18 +33,19 @@ Modify the User Model --------------------- By default, Laravel generates the ``User`` Eloquent model in your ``App/Models`` -directory. To enable authentication for users stored in MongoDB, your ``User`` model +directory. To enable authentication for MongoDB users, your ``User`` model must extend the ``MongoDB\Laravel\Auth\User`` class. To extend this class, navigate to your ``app/Models/User.php`` file and replace the -``use Illuminate\Foundation\Auth\User as Authenticatable`` use statement with the following +``use Illuminate\Foundation\Auth\User as Authenticatable`` statement with the following code: .. code-block:: php use MongoDB\Laravel\Auth\User as Authenticatable; -Ensure that your ``User`` class extends ``Authenticatable``, as shown in the following code: +Next, ensure that your ``User`` class extends ``Authenticatable``, as shown in the following +code: .. code-block:: php @@ -53,13 +54,13 @@ Ensure that your ``User`` class extends ``Authenticatable``, as shown in the fol ... } -After configuring your ``User`` model, create a corresponding controller. For instructions on -creating a controller, see the :ref:`laravel-auth-controller` section on this page. +After configuring your ``User`` model, create a corresponding controller. To learn how to +create a controller, see the :ref:`laravel-auth-controller` section on this page. Example ~~~~~~~ -The following code demonstrates a ``User.php`` file that extends the ``MongoDB\Laravel\Auth\User`` +The following code shows a ``User.php`` file that extends the ``MongoDB\Laravel\Auth\User`` class: .. literalinclude:: /includes/auth/AuthUser.php