基于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(['email' => $email, 'password' => $password])) {
// Authentication passed...
return redirect()->intended('dashboard');
}
}
}
這個方法會根據你傳入的參數判斷數據庫中是否存在與之相匹配的用戶,如果存在并且密碼正確返回true,反之返回false
遂在LoginController中添加該方法,但是好像并沒有效果
于是開始觀察LoginController的實現機制,發(fā)現它實現了一個AuthenticatesUsers的trait,追蹤到這個trait的定義文件,發(fā)現這個文件就是我們想要的東西
里面有一個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);
}
分析了一波這個文件,發(fā)現主要進行登錄判斷的就是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('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;
}
只需要用attempt方法進行多次判斷即可,只要成功就返回true,不成功繼續(xù)用其他字段進行判斷,都不成功則返回flase
測試,可以實現多字段登錄效果
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
PHP 中 Orientation 屬性判斷上傳圖片是否需要旋轉
本文給大家介紹使用php技術實現根據上傳圖片orientation屬性判斷是否需要旋轉,感興趣的朋友一起看看吧2015-10-10
ThinkPHP5郵件發(fā)送服務封裝(可發(fā)附件)
這篇文章主要介紹了ThinkPHP5封裝郵件發(fā)送服務,可發(fā)附件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
laravel5.0在linux下解決.htaccess無效和去除index.php的問題
今天小編就為大家分享一篇laravel5.0在linux下解決.htaccess無效和去除index.php的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Yii實現單用戶博客系統(tǒng)文章詳情頁插入評論表單的方法
這篇文章主要介紹了Yii實現單用戶博客系統(tǒng)文章詳情頁插入評論表單的方法,結合實例分析了Yii實現文章詳情頁評論表單功能的具體技巧,需要的朋友可以參考下2015-12-12

