2016-02-22 50 views
2

我試圖設置兩個身份驗證警衛:internal(用於正常瀏覽器請求)和api用於AJAX請求。 api是默認的後衛,但我現在專注於讓internal-guard工作。在Laravel 5中使用多個身份驗證警告時重定向循環

這是我的配置/ auth.php:

<?php 

return [ 
    'defaults' => [ 
     'guard' => 'api', 
     'passwords' => 'clients', 
    ], 

    'guards' => [ 
     'internal' => [ 
      'driver' => 'session', 
      'provider' => 'users', 
     ], 
     'api' => [ 
      'driver' => 'token', 
      'provider' => 'clients', 
     ], 
    ], 

    'providers' => [ 
     'users' => [ 
      'driver' => 'eloquent', 
      'model' => App\User::class, 
     ], 
     'clients' => [ 
      'driver' => 'eloquent', 
      'model' => App\Client::class, 
     ], 
    ], 

    'passwords' => [ 
     'users' => [ 
      'provider' => 'users', 
      'email' => 'auth.emails.password', 
      'table' => 'password_resets', 
      'expire' => 60, 
     ], 
    ], 

]; 

這是我的routes.php文件:

<?php 
Route::group([ 
    'domain' => 'internal.example.com', 
    'middleware' => ['web', 'auth:internal'] 
], function() { 
    Route::get('/', function() { 
     return view('welcome'); 
    }); 
    Route::get('/home', '[email protected]'); 
}); 

Route::group([ 
    'domain' => 'internal.example.com', 
    'middleware' => [ 'web'] 
], function() { 
    Route::match(['get', 'post'], '/login', 'InternalAuth\[email protected]'); 
    Route::get('/logout', 'InternalAuth\[email protected]'); 
}); 

這是InternalAuthController:

<?php 

namespace App\Http\Controllers\InternalAuth; 

use App\User; 
use Validator; 
use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\ThrottlesLogins; 
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; 

class InternalAuthController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Registration & Login Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles the registration of new users, as well as the 
    | authentication of existing users. By default, this controller uses 
    | a simple trait to add these behaviors. Why don't you explore it? 
    | 
    */ 

    use AuthenticatesAndRegistersUsers, ThrottlesLogins; 

    /** 
    * Where to redirect users after login/registration. 
    * 
    * @var string 
    */ 
    protected $redirectTo = '/'; 

    protected $guard = 'internal'; 

    /** 
    * Create a new authentication controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('guest', ['except' => 'logout']); 
    } 

    /** 
    * Get a validator for an incoming registration request. 
    * 
    * @param array $data 
    * @return \Illuminate\Contracts\Validation\Validator 
    */ 
    protected function validator(array $data) 
    { 
     return Validator::make($data, [ 
      'name' => 'required|max:255', 
      'email' => 'required|email|max:255|unique:users', 
      'password' => 'required|confirmed|min:6', 
     ]); 
    } 

    /** 
    * Create a new user instance after a valid registration. 
    * 
    * @param array $data 
    * @return User 
    */ 
    protected function create(array $data) 
    { 
     return User::create([ 
      'name' => $data['name'], 
      'email' => $data['email'], 
      'password' => bcrypt($data['password']), 
     ]); 
    } 
} 

似乎沒什麼問題。但是當我在瀏覽器中進入/,/ home或/ login時,我最終進入了一個重定向循環。 我錯過了什麼...任何想法?

+0

刪除AUTH中間件, '登陸' 等,只有實現對那些需要授權用戶的路由。 –

+0

在'/'上設置了Auth中間件。 '/ login'上沒有auth中間件。但是我玩過並注意到,當'/'不受保護時,'/ login'會重定向到'/'。所以這導致了重定向循環。但是,當我沒有通過身份驗證時,爲什麼'/ login'這樣做呢? – tillsanders

+0

在中間件中檢查auth.php或Authorization.php。 –

回答

0

/login指向InternalAuth\[email protected]login()Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsersuse d by InternalAuth)的一種方法,但它不負責返回視圖或響應帖子請求。這是getLoginpostLogin的工作。

所以,我需要在routes.php可以改變:

Route::match(['get', 'post'], '/login', 'InternalAuth\[email protected]'); 

要這樣:從像 '/' 你的大衆路線

Route::get('/login', 'InternalAuth\[email protected]'); 
Route::post('/login', 'InternalAuth\[email protected]');