-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 5 commits
d98f396
b09e4c2
a7e7b20
f1f0df2
85db22a
c03fc05
3c3cfdf
6dff654
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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']); | ||
} | ||
} |
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 | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
protected $connection = 'mongodb'; | ||
protected $collection = 'users'; | ||
|
||
protected $fillable = [ | ||
'name', | ||
'email', | ||
'password', | ||
]; | ||
|
||
protected $hidden = [ | ||
'password', | ||
'remember_token', | ||
]; | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||||||
.. _laravel-user-authentication: | ||||||||||
|
||||||||||
=================== | ||||||||||
User authentication | ||||||||||
User Authentication | ||||||||||
=================== | ||||||||||
|
||||||||||
.. facet:: | ||||||||||
|
@@ -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. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: applies throughout
Suggested change
|
||||||||||
|
||||||||||
Laravel provides a native Auth module that includes authentication services, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||
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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
code: | ||||||||||
|
||||||||||
.. code-block:: php | ||||||||||
|
||||||||||
use MongoDB\Laravel\Auth\User as Authenticatable; | ||||||||||
|
||||||||||
Ensure that your ``User`` class extends ``Authenticatable``, as shown in the following code: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||
|
||||||||||
.. 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. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I: on/about
Suggested change
|
||||||||||
|
||||||||||
Example | ||||||||||
~~~~~~~ | ||||||||||
|
||||||||||
The following code demonstrates a ``User.php`` file that extends the ``MongoDB\Laravel\Auth\User`` | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: shows? displays? "demonstrates" is more active to me
Suggested change
|
||||||||||
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 | ||||||||||
]; | ||||||||||
|
||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
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. |
Uh oh!
There was an error while loading. Please reload this page.