Home >Backend Development >PHP Tutorial >Detailed explanation of how to implement multi-field login in Laravel5.4
Recently I encountered a need at work. I need to realize the effect of multi-field login, that is, you can log in using any method of mobile phone or email. Now I will share the solution process, so this article mainly introduces it to you. The relevant information on implementing the multi-field login function based on Laravel5.4 is available. Friends who need it can refer to it. Let’s take a look together. I hope to be helpful.
Preface
Recently, I needed to implement a multi-field login function in a project. To put it simply, you can use username, email or mobile phone Log in any way. So this article will introduce to you the relevant content about Laravel5.4 multi-field login, and share it for your reference and study. Without further ado, let’s take a look at the detailed introduction.
The following content is based on laravel5.4
The method is as follows:
First, use the artisan tool Generate auth module
php artisan make:auth
At this time, an Auth directory will be added to the App\Http\Controllers directory, which is related to registration and login. In the controller, the resources\views directory will also generate some views related to registration and login
Laravel's official documentation says that manual authentication of users requires the attempt method of the Illuminate\Support\Facades\Auth class, as follows:
<?php namespace App\Http\Controllers; use Illuminate\Support\Facades\Auth; class LoginController extends Controller { /** * Handle an authentication attempt. * * @return Response */ public function authenticate() { if (Auth::attempt(['email' => $email, 'password' => $password])) { // Authentication passed... return redirect()->intended('dashboard'); } } }
This method will determine whether there is a matching user in the database based on the parameters you passed in. If it exists and the password is correct, it will return true, otherwise it will return false
Then add the user in LoginController method, but it seemed to have no effect
So we started to observe the implementation mechanism of LoginController and found that it implemented a trait of AuthenticatesUsers. We traced the definition file of this trait and found that this file is what we want
There is a login method in it, which is responsible for processing the logic of login
/** * Handle a login request to the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response */ public function login(Request $request) { // 表单验证 $this->validateLogin($request); // If the class is using the ThrottlesLogins trait, we can automatically throttle // the login attempts for this application. We'll key this by the username and // the IP address of the client making these requests into this application. // 防止暴力破解,多次登录失败会根据IP锁定 if ($this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } // 这个就是主要的负责判断数据库中是否存在相应的账号和密码的地方,我们需要重写的就是attemptLogin方法 if ($this->attemptLogin($request)) { return $this->sendLoginResponse($request); } // If the login attempt was unsuccessful we will increment the number of attempts // to login and redirect the user back to the login form. Of course, when this // user surpasses their maximum number of attempts they will get locked out. // 登录失败,失败次数++,防止暴力破解 $this->incrementLoginAttempts($request); // 返回失败响应 return $this->sendFailedLoginResponse($request); }
After analyzing a wave of this file, I found that the main method for determining login is the attemptLogin method. We only need to rewrite this method. Let’s look at it first. See how the original one is written, and rewrite it according to the original one:
/** * Attempt to log the user into the application. * * @param \Illuminate\Http\Request $request * @return bool */ protected function attemptLogin(Request $request) { return $this->guard()->attempt( $this->credentials($request), $request->has('remember') ); }
After the LoginController is rewritten:
public function attemptLogin(Request $request) { $username = $request->input('username'); $password = $request->input('password'); // 验证用户名登录方式 $usernameLogin = $this->guard()->attempt( ['username' => $username, 'password' => $password], $request->has('remember') ); if ($usernameLogin) { return true; } // 验证手机号登录方式 $mobileLogin = $this->guard()->attempt( ['mobile' => $username, 'password' => $password], $request->has('remember') ); if ($mobileLogin) { return true; } // 验证邮箱登录方式 $emailLogin = $this->guard()->attempt( ['email' => $username, 'password' => $password], $request->has('remember') ); if ($emailLogin) { return true; } return false; }
Just use the attempt method to make multiple judgments, as long as it succeeds Return true, if unsuccessful, continue to use other fields to judge, if unsuccessful, return false
Test, you can achieve multi-field login effect
Related recommendations:
How to use the corresponding interface of Laravel 5.5?
Laravel 5.5 implements front and backend login
Laravel repeatedly executes the same queue task reason
The above is the detailed content of Detailed explanation of how to implement multi-field login in Laravel5.4. For more information, please follow other related articles on the PHP Chinese website!