簡體   English   中英

LARAVEL5自定義登錄

[英]LARAVEL5 Custom login

我正在使用需要自定義登錄的應用程序。

我要遵循這個流程。

  1. 用戶將進入登錄頁面。
  2. 用戶提交登錄頁面。
  3. 應用程序將檢查用戶是否在數據庫3.1中(如果用戶不在數據庫中|它將向第三方發送請求並檢查登錄是否成功)3.2如果用戶在數據庫中驗證密碼。

現在我已經為第三方完成了課程,代碼將像這樣工作

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

如果登錄成功, $third->login將返回true。

現在問題是如何鏈接這個邏輯。 使用laravel預定義函數Auth::check()

當您安裝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']);
    }

}

此類使用存儲在以下位置的登錄特征: vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Auth\\AuthenticatesAndRegistersUsers.php

你可以覆蓋這個類中的方法來放置你自己的邏輯,例如在AuthController類中你可以定義一個新的:

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

它會尊重你的功能而不是特質功能。 無論如何,來自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
    }

你可以做兩件事:

  1. 編輯特質本身(不好的做法)來建立你自己的邏輯
  2. AuthController定義您自己的postLogin函數並復制邏輯,但使用您自己的自定義邏輯對其進行編輯。

編輯以更加適合您的觀點:

  1. 用戶將進入登錄頁面:您可以使用laravel為您提供的默認登錄頁面,或者您可以將getLogin函數和redircet覆蓋到您自己的視圖中。

  2. 用戶提交登錄頁面:表單操作需要: {{ url('/auth/login') }}或者你放到postLogin()路徑

  3. 應用程序將檢查用戶是否在數據庫中:在代碼行1中

    3.1(如果用戶不在數據庫中|它將向第三方發送請求並檢查登錄是否成功):在代碼行3中

3.2如果用戶在數據庫中驗證密碼:在代碼行2中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM