首頁  >  文章  >  後端開發  >  詳解Laravel5.4實作多字段登入的方法

詳解Laravel5.4實作多字段登入的方法

*文
*文原創
2018-01-03 15:28:411315瀏覽

最近在工作中遇到一個需求,需要實現多字段登錄的一個效果,就是可以使用手機或郵箱任一種方式的登錄,現在將解決的過程分享出來,所以這篇文章主要給大家介紹了基於Laravel5.4實作多字段登入功能的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。希望對大家有幫助。

前言

最近在一個專案中需要實作一個多欄位登入功能,簡單來說就是可以使用使用者名稱、信箱或手機號任意一種方式進行登入。所以本文來跟大家介紹了Laravel5.4多字段登入的相關內容,分享出來供大家參考學習,話不多說了,來一起看看詳細的介紹吧。

以下內容基於laravel5.4

#方法如下:

首先,透過artisan工具產生auth模組

php artisan make:auth

#這時候App\Http\Controllers目錄下會新增一個Auth目錄,該目錄下為註冊登入相關的控制器,resources\views目錄下方也會產生一些與註冊登入相關的視圖

laravel的官方文件中說手動認證使用者需要使用Illuminate\Support\Facades\Auth類別的attempt方法,如下:

<?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;);
  }
 }
}

這個方法會根據你傳入的參數判斷資料庫中是否存在與之相符的用戶,如果存在且密碼正確回傳true,反之回傳false

遂在LoginController中加入該方法,但好像沒有效果

於是開始觀察LoginController的實作機制,發現它實作了一個AuthenticatesUsers的trait,追蹤到這個trait的定義文件,發現這個文件就是我們想要的東西

裡面有一個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);
 }

分析了一波這個文件,發現主要進行登入判斷的就是attemptLogin方法,我們只要重寫這個方法即可,先看看原來的是怎麼寫的,根據原來的進行重寫:

/**
  * 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;)
  );
 }

在LoginController重寫後:

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;
 }

只需要用attempt方法進行多次判斷即可,只要成功就回傳true,不成功繼續用其他欄位進行判斷,都不成功則回傳flase

測試,可以實現多欄位登入效果

相關推薦:

Laravel 5.5的可對應介面如何使用?

Laravel 5.5實作前後台登陸

Laravel 重複執行同一個佇列任務的原因

以上是詳解Laravel5.4實作多字段登入的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn