Home > Article > Backend Development > Detailed explanation of the Auth module in the Laravel framework
I recently encountered the Auth module at work, but I know little about the Auth module. I learned about it by looking for relevant information, so the following article mainly introduces you to the relevant information about the Auth module in Laravel, which is introduced through sample code. It is very detailed, friends who need it can refer to it, let’s take a look below.
Preface
This article mainly introduces to you the relevant content about the Auth module in Laravel, and shares it for your reference and study. The following words Not much more to say, let’s take a look at the detailed introduction.
This article is based on the analysis and writing of the localized module code of Laravel 5.4 version;
Module composition
The Auth module is functionally divided into two parts: user authentication and permission management; in terms of file composition, the Illuminate\Auth\Passwords directory is a small module for password reset or forgotten password processing, and Illuminate\Auth is responsible for user authentication and permission management. module, Illuminate\Foundation\Auth provides a series of specific logic implementations such as login, password modification, password reset, etc.;
The following figure shows the relationship between the various files of the Auth module and gives a brief explanation ;
User authentication
HTTP itself is stateless, usually in During system interaction, the account or Token identification is used to determine the authenticated user;
Configuration file interpretation
return [ 'defaults' => [ 'guard' => 'web', ... ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], ], ], ];
From bottom to top, understand;
providers is an interface that provides user data, and the driver object and target object must be marked; here, the key name users is the name of a set of providers, driven by eloquent, and modal is App\User::class;
The guards part is configured for the authentication management part; there are two authentication methods, one is called web, and the other is api; web authentication is based on Session interaction, and the user ID is obtained based on sessionId. Query the user in the users provider; api authentication is based on token value interaction and also uses the users provider;
The defaults item shows that web authentication is used by default;
Authentication
Session binding authentication information:
// $credentials数组存放认证条件,比如邮箱或者用户名、密码 // $remember 表示是否要记住,生成 `remember_token` public function attempt(array $credentials = [], $remember = false) public function login(AuthenticatableContract $user, $remember = false) public function loginUsingId($id, $remember = false)
HTTP basic authentication, authentication information is placed in the request header; subsequent request access Pass sessionId;
public function basic($field = 'email', $extraConditions = [])
Only authenticates in the current session, and does not record authentication information in the session:
public function once(array $credentials = []) public function onceUsingId($id) public function onceBasic($field = 'email', $extraConditions = [])
During the authentication process (including registration, forgotten password), the defined events are as follows:
Event name | Description |
---|---|
Attempt to verify the event | |
Verification passed event | |
Verification failed event | |
The number of failures exceeds the limit, lock the request to access the event again | |
Successfully logged in through 'remember_token' When, the event called | |
User Exit Event | |
User Registration Event |
The above is the detailed content of Detailed explanation of the Auth module in the Laravel framework. For more information, please follow other related articles on the PHP Chinese website!