Laravel5.4多字段登陆

Laravel5.4多字段登陆

最近在一个项目中须要实现一个多字段登陆功能,就是能够使用用户名、邮箱或手机号任意一种方式进行登陆。php

如下内容基于laravel5.4laravel

首先,经过artisan工具生成auth模块数据库

php artisan make:authapp

这时候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,反之返回falsethis

遂在LoginController中添加该方法,可是好像并无效果spa

因而开始观察LoginController的实现机制,发现它实现了一个AuthenticatesUserstrait,追踪到这个trait的定义文件,发现这个文件就是咱们想要的东西code

里面有一个login方法,就是负责处理登陆的逻辑orm

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

分析了一波这个文件,发现主要进行登陆判断的就是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,不成功继续用其余字段进行判断,都不成功则返回flase

测试,能够实现多字段登陆效果

相关文章
相关标签/搜索