2015-06-04 76 views
3

我在需要自定義登錄的應用程序中工作。LARAVEL5自定義登錄

我必須遵循這個流程。

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

現在我已經做到類第三方和代碼將作爲本

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

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

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

+0

你認爲哪些事情比默認auth與laravel有更好的解決方案? –

+0

@SafoorSafdar好吧,拉拉維爾的Auth出來了。通常使用這種類型的身份驗證。但是如果你需要任何額外的東西或重新邏輯的東西。你需要這樣做。在我看來,卡洛斯說,我認爲「更好的解決方案」。 – xdevnull

回答

3

當您安裝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 
} 

它會尊重你的功能,而不是特性功能。 反正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 
    } 

你可以做兩件事情:

  1. 編輯性狀本身(糟糕的做法)把自己的邏輯
  2. 定義您自己的postLogin函數AuthController並複製邏輯,但用您自己的自定義邏輯進行編輯。

編輯 更conrete與你的觀點:

  1. 用戶會進入登錄頁面:您可以使用默認的登錄頁面,laravel給你,或者你可以覆蓋getLogin功能redircet你自己的看法。

  2. 用戶提交的登錄頁面:表單動作必須是:{{ url('/auth/login') }}或任何路線,你投入postLogin()

  3. 應用,如果用戶在數據庫將檢查:在代碼行1

    3 。1(如果用戶沒有在數據庫|它會發送到第三方的請求,並檢查是否登錄成功):在代碼行3

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

+0

你想知道在加載登錄頁面之前是否有用戶登錄? –