Laravel 5 athentication in subdomain is not workin

2019-01-29 08:44发布

问题:

I am using a subdomain in my laravel 5 project but when I try to loggin a user, it won't authenticate anymore. This is my 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,
];

my routes.php

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

this is on the 'DashController@index' on DashController.php

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

but when signing in, it won't redirect on it. this make me issue. I also check by var_dump(Auth::user()) but it returns null value but I tried var_dump($request->session()->get('uid')) it returns the user_id of the user who is trying to authenticate.

my question is, did I missed something? I just want the user to be authenticated to be in the Auth::user(). please help me with this. any help will be appreciated. thank you in advance.

by the way, this is my code for loggin a 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:

it works now. it just a routes issue on my code. the subdomain in routes.php must be the first and inside on it will be the prefix and etc. like this:

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');


标签: php laravel-5