Detailed explanation of the Auth module in the Laravel framework
巴扎黑
Release: 2023-03-15 11:40:01
Original
3221 people have browsed it
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;
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)
Copy after login
HTTP basic authentication, authentication information is placed in the request header; subsequent request access Pass sessionId;
public function basic($field = 'email', $extraConditions = [])
Copy after login
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 = [])
Copy after login
During the authentication process (including registration, forgotten password), the defined events are as follows:
Event name
Description
##Attempting
Attempt to verify the event
Authenticated
Verification passed event
Failed
Verification failed event
Lockout
The number of failures exceeds the limit, lock the request to access the event again
Logi
Successfully logged in through 'remember_token' When, the event called
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!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn