且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LARAVEL5自定义登录

更新时间:2022-05-08 05:34:46

在安装laravel时,它带有一个默认登录名,该登录名使用特征:

When you install laravel, it comes with a default login, that uses a trait:

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

}

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

this class use the trait for login stored in: vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php

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

you can overwrite the methods from this class to put your own logic, for example in the class AuthController you can define a new:

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

它会尊重您的功能,而不是特质功能. 无论如何,auth traitpostLogin背后的逻辑是:

and it gonna respect your function instead the trait funcion. anyway, the logic behind the postLogin from auth trait is:

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
    }

您可以做两件事:

  1. 编辑特征本身(不好的做法),以表达自己的逻辑
  2. AuthController中定义自己的postLogin函数并复制逻辑,但使用自己的自定义逻辑对其进行编辑.
  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.

修改 更加了解您的观点:

Edit to be more conrete with your points:

  1. 用户将进入登录页面:您可以使用laravel为您提供的默认登录页面,也可以覆盖getLogin函数并重新显示为您自己的视图.

  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.

用户提交登录页面:表单操作必须为:{{ url('/auth/login') }}或您放置到postLogin()

User submit login page: the form action needs to be: {{ url('/auth/login') }} or whatever route you put to postLogin()

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

Application will check if the user is in database: in the code line 1

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

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.2如果用户在数据库中,请验证密码:在代码行2中

3.2 If user is in database verify password: in the code line 2