About Laravel5.4 implementation of multi-field login method

Source: Internet
Author: User
This article mainly introduces the Laravel5.4 implementation of multi-field login method, has a certain reference value, now share to everyone, the need for friends can refer to

Recently encountered a demand in the work, the need to achieve a multi-field login effect, that can use the phone or a mailbox in either way to log in, now the process to share the solution, So this article mainly introduces to you based on the Laravel5.4 implementation of multi-field login function of the relevant information, the need for friends can reference, the following to see together.

Objective

Recently in a project you need to implement a multi-field login function, simply means you can use a user name, mailbox or phone number in any way to sign in. So this article to introduce to you about Laravel5.4 multi-field login related content, share out for everyone reference study, words not to say, come together to see the detailed introduction bar.

The following content is based on laravel5.4

Here's how:

First, generate the Auth module from the artisan tool

php artisan make:auth

At this time, the App\http\controllers directory will be added a auth directory, the directory for the registration of the relevant controller, Resources\views directory will also generate a number of registered logins related to the view

Laravel's official documentation says manual authentication requires users to use the attempt method of the Illuminate\support\facades\auth class, as follows:

<?phpnamespace app\http\controllers;use illuminate\support\facades\auth;class LoginController extends Controller {/**  * Handle an authentication attempt.  *  * @return Response */public  function authenticate () {  if (auth::attempt ("e-mail" = $email, ' password ' = = $password])) {   //authentication passed   ... Return Redirect ()->intended (' dashboard ');  } }}

This method will determine if there is a matching user in the database based on the parameters you passed in, and if it exists and the password returns true correctly, the reverse returns false.

The method is added to the Logincontroller, but it seems to have no effect

Then began to observe the implementation mechanism of Logincontroller, found that it implemented a authenticatesusers trait, traced to the trait definition file, found that this file is what we want

There is a login method, which is responsible for handling the login logic

/** * Handle A login request to the application. * * @param \illuminate\http\request $request * @return \illuminate\http\redirectresponse|\illuminate\http\response */P  ublic function Login (Request $request) {//form validation $this->validatelogin ($request); If the class is using the throttleslogins trait, we can automatically throttle//The login attempts for this applicat Ion.  We ' ll key this by the username and//The IP address of the client making these requests to this application.   Prevent brute force, multiple logon failures will be based on IP lock if ($this->hastoomanyloginattempts ($request)) {$this->firelockoutevent ($request);  return $this->sendlockoutresponse ($request); }//This is the main responsible for determining whether the database has the corresponding account and password, we need to rewrite the Attemptlogin method if ($this->attemptlogin ($request)) {return $this-&gt  ; Sendloginresponse ($request); }//If The login attempt was unsuccessful we'll increment the number of attempts//to login and redirect the user Ba CK to the login form. Of course, when this/user SurpaSSEs their maximum number of attempts they would get locked out.  Login failed, number of failures + +, to prevent brute force cracking $this->incrementloginattempts ($request); Return failed response return $this->sendfailedloginresponse ($request); }

Analyzed a wave of this file, found that the main login to judge is the Attemptlogin method, we just rewrite this method can be, first look at the original is how to write, according to the original rewrite:

/**  * 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 rewrite:

Public Function Attemptlogin (Request $request) {  $username = $request->input (' username ');  $password = $request->input (' password ');  Verify the user name login method  $usernameLogin = $this->guard ()->attempt (   [' username ' = + $username, ' password ' = = $ Password], $request->has (' Remember ')  );  if ($usernameLogin) {   return true;  }  Verify that the phone number  is logged in $mobileLogin = $this->guard ()->attempt (   [' mobile ' + $username, ' password ' = ' $ Password], $request->has (' Remember ')  );  if ($mobileLogin) {   return true;  }  Verify how the mailbox is logged on  $emailLogin = $this->guard ()->attempt (   [' email ' + $username, ' password ' = ' $ ' Password], $request->has (' Remember ')  );  if ($emailLogin) {   return true;  }  return false; }

You only need to use the attempt method to make multiple judgments, as long as the success of the return true, unsuccessful continue to judge with other fields, are unsuccessful then return flase

Test, you can achieve multi-field login effect

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.