LARAVEL5自定义登录

xde*_*ull 4 php laravel

我正在使用需要自定义登录的应用程序.

我要遵循这个流程.

  1. 用户将进入登录页面.
  2. 用户提交登录页面.
  3. 应用程序将检查用户是否在数据库3.1中(如果用户不在数据库中|它将向第三方发送请求并检查登录是否成功)3.2如果用户在数据库中验证密码.

现在我已经为第三方完成了课程,代码将像这样工作

$third = new Libraries\ThirdParty();
$third->login($username, $password);
Run Code Online (Sandbox Code Playgroud)

$third->login 如果登录成功,将返回true.

现在问题是如何链接这个逻辑.具有laravel预定义功能Auth::check()

Car*_*ata 6

当您安装laravel时,它会附带一个使用特征的默认登录:

class AuthController extends Controller {

    use AuthenticatesAndRegistersUsers;

    /**
     * Create a new authentication controller instance.
     *
     * @param  \Illuminate\Contracts\Auth\Guard  $auth
     * @param  \Illuminate\Contracts\Auth\Registrar  $registrar
     * @return void
     */
    public function __construct(Guard $auth, Registrar $registrar)
    {
        $this->auth = $auth;
        $this->registrar = $registrar;

        $this->middleware('guest', ['except' => 'getLogout']);
    }

}
Run Code Online (Sandbox Code Playgroud)

此类使用特征进行登录存储在: vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php

您可以覆盖此类中的方法以放置您自己的逻辑,例如在AuthController您可以定义新类的类中:

function postLogin(){
   //your new logic for login
}
Run Code Online (Sandbox Code Playgroud)

它会尊重你的功能而不是特质功能.反正背后的逻辑postLoginauth trait是:

public function postLogin(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email', 'password' => 'required',
        ]);

        $credentials = $request->only('email', 'password');

        if ($this->auth->attempt($credentials, $request->has('remember')))
        { //this if validate if the user is on the database line 1
            return redirect()->intended($this->redirectPath());
            //this redirect if user is the db line 2
        }

        return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors([
                        'email' => $this->getFailedLoginMessage(),
                    ]);
          //redirect again to login view with some errors line 3
    }
Run Code Online (Sandbox Code Playgroud)

你可以做两件事:

  1. 编辑特质本身(不好的做法)来放置自己的逻辑
  2. 定义您自己的postLogin函数AuthController并复制逻辑,但使用您自己的自定义逻辑对其进行编辑.

编辑 以更加适合您的观点:

  1. 用户将进入登录页面:您可以使用laravel为您提供的默认登录页面,或者您可以将getLogin功能和redircet 覆盖到您自己的视图中.

  2. 用户提交登录页面:表单操作需要:{{ url('/auth/login') }}或者您输入的路径postLogin()

  3. 应用程序将检查用户是否在数据库中:在代码行1中

    3.1(如果用户不在数据库中|它将向第三方发送请求并检查登录是否成功):在代码行3中

3.2如果用户在数据库中验证密码:在代码行2中