Skip to content

DOCSP-38100: User authentication #3034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/includes/auth/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

use function response;

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']);
}
}
22 changes: 22 additions & 0 deletions docs/includes/auth/AuthUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Models;

use MongoDB\Laravel\Auth\User as Authenticatable;

class User extends Authenticatable
{
protected $connection = 'mongodb';
protected $collection = 'users';

protected $fillable = [
'name',
'email',
'password',
];

protected $hidden = [
'password',
'remember_token',
];
}
123 changes: 116 additions & 7 deletions docs/user-authentication.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. _laravel-user-authentication:

===================
User authentication
User Authentication
===================

.. facet::
Expand All @@ -11,14 +11,123 @@ User authentication
.. meta::
:keywords: php framework, odm, code example

If you want to use Laravel's native Auth functionality, register this included
service provider:
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

Overview
--------

In this guide, you can learn how to authenticate users stored in MongoDB
by using Laravel's native authentication functionality.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: applies throughout

Suggested change
In this guide, you can learn how to authenticate users stored in MongoDB
by using Laravel's native authentication functionality.
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: not sure whether this is standard for PHP/Laravel, but I'd guess it helps readability

Suggested change
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
<https://laravel.com/docs/{+laravel-docs-version+}/authentication>`__ in the
Laravel documentation.

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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: Might help to link this step to the previous one

Suggested change
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

class User extends Authenticatable
{
...
}

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I: on/about

Suggested change
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``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: shows? displays? "demonstrates" is more active to me

Suggested change
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
:language: php
:dedent:

.. _laravel-auth-controller:

User Controller
---------------

To store functions that manage authentication, create an authentication controller for
your ``User`` model.

Run the following command from your project root to create a controller:

.. code-block:: php

php artisan make:controller <filename>

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: 4

return [
App\Providers\AppServiceProvider::class,
MongoDB\Laravel\MongoDBServiceProvider::class,
MongoDB\Laravel\Auth\PasswordResetServiceProvider::class
];

MongoDB\Laravel\Auth\PasswordResetServiceProvider::class,
Additional Information
----------------------

This service provider will slightly modify the internal ``DatabaseReminderRepository``
to add support for MongoDB based password reminders.
To learn more about user authentication, see `Authentication <https://laravel.com/docs/{+laravel-docs-version+}/authentication>`__
in the Laravel documentation.

If you don't use password reminders, you can omit this service provider.
To learn more about Eloquent models, see the :ref:`laravel-eloquent-model-class` guide.