2016-02-22 25 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]');