About how to implement multi-field login in Laravel5.4

不言
Release: 2023-04-01 06:32:02
Original
1008 people have browsed it

This article mainly introduces the method of implementing multi-field login in Laravel5.4. It has certain reference value. Now I share it with you. Friends in need can refer to it.

I encountered it at work recently One requirement is 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 to you the implementation of multi-field login based on Laravel5.4. Friends who need it can refer to the relevant information about the functions. Let’s take a look below.

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

The official documentation of laravel 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([&#39;email&#39; => $email, &#39;password&#39; => $password])) {
   // Authentication passed...
   return redirect()->intended(&#39;dashboard&#39;);
  }
 }
}
Copy after login

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&#39;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);
 }
Copy after login

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(&#39;remember&#39;)
  );
 }
Copy after login

After the LoginController is rewritten:

public function attemptLogin(Request $request)
 {
  $username = $request->input(&#39;username&#39;);
  $password = $request->input(&#39;password&#39;);

  // 验证用户名登录方式
  $usernameLogin = $this->guard()->attempt(
   [&#39;username&#39; => $username, &#39;password&#39; => $password], $request->has(&#39;remember&#39;)
  );
  if ($usernameLogin) {
   return true;
  }

  // 验证手机号登录方式
  $mobileLogin = $this->guard()->attempt(
   [&#39;mobile&#39; => $username, &#39;password&#39; => $password], $request->has(&#39;remember&#39;)
  );
  if ($mobileLogin) {
   return true;
  }

  // 验证邮箱登录方式
  $emailLogin = $this->guard()->attempt(
   [&#39;email&#39; => $username, &#39;password&#39; => $password], $request->has(&#39;remember&#39;)
  );
  if ($emailLogin) {
   return true;
  }

  return false;
 }
Copy after login

Just use the attempt method to make multiple judgments, as long as it succeeds Return true. If it is unsuccessful, continue to use other fields to judge. If it is unsuccessful, it will return fle

Test, which can achieve multi-field login effect

The above is the entire content of this article. I hope it will be useful to everyone. Helpful, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

How to implement SMS registration with Laravel

How to use Laravel to implement scheduled tasks

The above is the detailed content of About how to implement multi-field login in Laravel5.4. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!