溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用Laravel5.4怎么實現一個多字段登錄功能

發布時間:2021-02-05 16:23:23 來源:億速云 閱讀:151 作者:Leah 欄目:開發技術

今天就跟大家聊聊有關利用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(['email' => $email, 'password' => $password])) {
   // Authentication passed...
   return redirect()->intended('dashboard');
  }
 }
}

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

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

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

里面有一個login方法,就是負責處理登錄的邏輯

/**

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

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

 protected function attemptLogin(Request $request)
 {
  return $this->guard()->attempt(
   $this->credentials($request), $request->has('remember')
  );
 }

在LoginController重寫后:

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

看完上述內容,你們對利用Laravel5.4怎么實現一個多字段登錄功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女