【问题标题】:Laravel 4 Authentication not working with SubdomainsLaravel 4 身份验证不适用于子域
【发布时间】:2014-03-10 00:15:05
【问题描述】:

在我的 L4 应用程序中,我使用子域来路由到不同的东西。

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.

如果有人访问 community.domain.com/forum 并且未通过身份验证,他应该被发送到 accounts.domain.com,登录然后被重定向回论坛。

但现在我有两个问题。 1和主要问题:登录后,用户仅对域进行身份验证:accounts.domain.com 对于所有其他域,他会被重定向到登录。 如果用户通过身份验证并尝试访问 dashboard.domain.com,他会被重定向到登录页面。

2.问题是登录后的重定向。 Atm 我在登录后只有一个静态重定向,不管用户来自哪里。我该如何更改它,以便他被重定向回他之前尝试以未经身份验证的用户身份访问的页面? 我的路线文件:

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

还有我的登录控制器:

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
}

感谢您的帮助。

【问题讨论】:

  • 在会话配置文件中,将'.mydomain.com' 放入domain 键中。
  • return Redirect::intended('foo/bar'); 将您带到您想去的地方,如果您不想去其他地方,则默认为foo/bar

标签: php laravel laravel-4


【解决方案1】:

app/config/session.php 中的domain 设置为.domain.com,以便在子域之间共享会话。

要重定向用户,您可以返回Redirect::back()Redirect::route(<wherever the user should land>)

【讨论】:

  • 我想在该设置中使用任何 TLD,我该怎么做?我尝试了一个通配符 ('domain' => '.domain.{tld}'),但它不是这样工作的。
猜你喜欢
  • 2021-12-20
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
  • 2016-08-04
  • 2013-04-17
  • 2017-02-04
相关资源
最近更新 更多