Laravel 4 Authentication not working with Subdomains

落花浮王杯 提交于 2019-12-06 07:20:17

问题


In my L4 App i use subdomains for my routing to different stuff.

accounts.domain.com = where alle the Authentication stuff happens
dashboard.domain.com = The main frontpage for authenticated users
community.domain.com = Community stuff for authenticated users.

If someone visits community.domain.com/forum and is not autenticated he should be send to accounts.domain.com, login and then get redirected back to the Forum.

But now i have 2 problems. 1 and major problem: afer the login the user is only autenticated for the domain: accounts.domain.com for all other domains he gets redirected to the login. If a user is autenticated and trys to access dashboard.domain.com he gets redirected to the login page.

and the 2. problem is the redirect after the login. Atm i just have a static redirect after the login, doesn't matter where the user was coming from. How can i change it so he get redirected back to the page he tried to visited as unauthenticated user before? My routes file:

Route::get('login', function()
{
    return Redirect::action('AccountsController@getLogin');
});

Route::group(array('domain' => 'accounts.domain.com'), function()
{
    Route::get('/', function()
    {
        return Redirect::action('AccountsController@getLogin');
    });

    Route::get('users/sing_in', array('as' => 'login', 'uses' => 'AccountsController@getLogin'));
    Route::post('users/sing_in', array('as' => 'login', 'uses' => 'AccountsController@doLogin'));
    Route::get('users/sing_out', array('as' => 'logout', 'uses' => 'AccountsController@doLogout'));


    Route::group(array('before' => 'auth'), function() {
        Route::get('users/profile', array('as' => 'profile', 'uses' => 'AccountsController@getProfile'));
    });

});


Route::group(array('domain' => 'dashboard.domain.com'), function()
{
    Route::group(array('before' => 'auth'), function() {
        Route::get('/', array('as' => 'dashhome', 'uses' => 'DashboardController@getIndex')); //If someone tries to access this, he get redirected to the login page, even if he just authenticated himself
    });
});

And my Login Controller:

public function getLogin()
{
    if (Auth::check()) {
        return Redirect::action('AccountsController@getProfile'); 
    } else {
        return View::make('login.index');
    }
}
public function doLogin()
{
    $rules = array(
        'email'    => 'required|email',
        'password' => 'required|min:3'
    );

    $validator = Validator::make(Input::all(), $rules);

    if ($validator->fails()) {
        return Redirect::route('login')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    } else {
        $userdata = array(
            'email'     => Input::get('email'),
            'password'  => Input::get('password')
        );
        if (Auth::attempt($userdata)) {
            return Redirect::action('AccountsController@getProfile');
        } else {
            return Redirect::route('login')->withErros('Wrong E-mail address or Password');
        }
    }
}
public function doLogout()
{
    Auth::logout(); // log the user out of our application
    return Redirect::route('login'); // redirect the user to the login screen
}

Thanks for any help.


回答1:


Set the domain in app/config/session.php to .domain.com, so a session gets shared between subdomains.

To redirect the user, you can return Redirect::back() or Redirect::route(<wherever the user should land>).



来源:https://stackoverflow.com/questions/22284173/laravel-4-authentication-not-working-with-subdomains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!