子域中的Laravel 5身份验证无法正常工作

时间:2016-09-27 00:46:08

标签: php laravel-5

我在laravel 5项目中使用子域名但是当我尝试登录用户时,它不再进行身份验证。这是我的config\session.php

return [
'driver' => env('SESSION_DRIVER', 'database'),
'lifetime' => 120,
'expire_on_close' => false,
'encrypt' => true,
'files' => storage_path('framework/sessions'),
'connection' => null,
'table' => 'sessions',
'lottery' => [2, 100],
'cookie' => 'laravel_session',
'path' => '/',
'domain' => '.stagedevelopment.dev',
'secure' => false,
'http_only' => true,
];

我的routes.php

Route::group([ 'prefix' => 'app', 'middleware' => 'auth' ], function() {
       ... some codes ....
    Route::group(['domain' => '{account_alias}.'.stagedevelopment.dev'], function () {
                    Route::get('dashboard', 'DashController@index');

这是在&{39; DashController@index'在DashController.php

public function index(Request $request, $account_alias = null){
   if ( Auth::user()){
    // CODES TO CALL THE VIEW
   }
}

但是在登录时,它不会重定向它。这让我受到质疑。我也是var_dump(Auth::user())检查,但它返回null值,但我尝试var_dump($request->session()->get('uid'))它会返回尝试进行身份验证的用户的user_id

我的问题是,我错过了什么吗?我只是想让用户通过身份验证Auth::user()。请帮我解决一下这个。任何帮助将不胜感激。提前谢谢你。

顺便说一下,这是我的用户登录代码

public function postSignIn( Request $request) {
        $rules = array(
            'email' => 'required|email',
            'password' => 'required',
        );
        $validator = Validator::make( Input::all(), $rules );
        if ( $validator->passes() ){
            $remember = ( !is_null( $request->get('remember')) ? true : false );
            if ( Auth::attempt( array( 'email' => $request->input('email'), 'password' => $request->input('password') ) ) ){
                if ( Auth::check() ){
                        $account = \App\Account::where( 'alias', 'LIKE', Auth::user()->account->alias )->get();
                        if($account->count() > 0){
                            return Redirect::to('http://'. Auth::user()->account->alias.'.stagedevelopment.dev/app/dashboard');
                        }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

现在有效。它只是我的代码上的路由问题。 routes.php中的子域必须是第一个,内部就是前缀等,如下所示:

Route::group(['domain' => '{account_alias}.'.env('APP_URL_NAME')], function () {
    // L3
    Route::group([ 'prefix' => 'app', 'middleware' => 'auth', 'middleware' => 'auth.manager' ], function() {
        Route::get('dashboard', 'DashController@index');