简体   繁体   English

LARAVEL5自定义登录

[英]LARAVEL5 Custom login

I'm working in application which requires a custom login. 我正在使用需要自定义登录的应用程序。

I've to follow this flow. 我要遵循这个流程。

  1. User will enter login page. 用户将进入登录页面。
  2. User submit login page. 用户提交登录页面。
  3. Application will check if the user is in database 3.1 (If user not in database | it will send a request to a third-party and check if login succeeded) 3.2 If user is in database verify password. 应用程序将检查用户是否在数据库3.1中(如果用户不在数据库中|它将向第三方发送请求并检查登录是否成功)3.2如果用户在数据库中验证密码。

Now i've done class for the third-party and the code will work as this 现在我已经为第三方完成了课程,代码将像这样工作

$third = new Libraries\ThirdParty();
$third->login($username, $password);

$third->login will return true if login succeeded. 如果登录成功, $third->login将返回true。

Now the question is how to link this logic. 现在问题是如何链接这个逻辑。 with the laravel pre-defined function Auth::check() 使用laravel预定义函数Auth::check()

When you install laravel, it comes with a default login, that uses a trait: 当您安装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']);
    }

}

this class use the trait for login stored in: vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Auth\\AuthenticatesAndRegistersUsers.php 此类使用存储在以下位置的登录特征: vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Auth\\AuthenticatesAndRegistersUsers.php

you can overwrite the methods from this class to put your own logic, for example in the class AuthController you can define a new: 你可以覆盖这个类中的方法来放置你自己的逻辑,例如在AuthController类中你可以定义一个新的:

function postLogin(){
   //your new logic for login
}

and it gonna respect your function instead the trait funcion. 它会尊重你的功能而不是特质功能。 anyway, the logic behind the postLogin from auth trait is: 无论如何,来自auth traitpostLogin背后的逻辑是:

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
    }

you can do two things: 你可以做两件事:

  1. edit the trait itself (bad practice) to put your own logic 编辑特质本身(不好的做法)来建立你自己的逻辑
  2. define your own postLogin function in AuthController and copy the logic but edit it with your own custom logic. AuthController定义您自己的postLogin函数并复制逻辑,但使用您自己的自定义逻辑对其进行编辑。

Edit to be more conrete with your points: 编辑以更加适合您的观点:

  1. User will enter login page: you can use the default login page that laravel gives you, or you can overwrite getLogin function and redircet to your own view. 用户将进入登录页面:您可以使用laravel为您提供的默认登录页面,或者您可以将getLogin函数和redircet覆盖到您自己的视图中。

  2. User submit login page: the form action needs to be: {{ url('/auth/login') }} or whatever route you put to postLogin() 用户提交登录页面:表单操作需要: {{ url('/auth/login') }}或者你放到postLogin()路径

  3. Application will check if the user is in database: in the code line 1 应用程序将检查用户是否在数据库中:在代码行1中

    3.1 (If user not in database | it will send a request to a third-party and check if login succeeded): in the code line 3 3.1(如果用户不在数据库中|它将向第三方发送请求并检查登录是否成功):在代码行3中

3.2 If user is in database verify password: in the code line 2 3.2如果用户在数据库中验证密码:在代码行2中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM