Laravel 4身份验证不适用于子域 [英] Laravel 4 Authentication not working with Subdomains

查看:63
本文介绍了Laravel 4身份验证不适用于子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的L4应用程序中,我使用子域路由到其他内容.

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.

如果有人访问community.domain.com/forum并没有发现问题,则应将其发送到account.domain.com,登录,然后将其重定向回论坛.

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.

但是现在我有2个问题. 1,主要问题:登录后仅针对以下用户登录该域:accounts.domain.com 对于所有其他域,他将被重定向到登录名. 如果用户被限制使用并尝试访问dashboard.domain.com,他将被重定向到登录页面.

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.

和2.问题是登录后的重定向. Atm登录后我只有一个静态重定向,与用户来自何处无关.我如何更改它,以便他被重定向回他曾尝试以未经身份验证的用户访问的页面? 我的路线文件:

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

还有我的登录控制器:

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
}

感谢您的帮助.

推荐答案

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

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

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

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

这篇关于Laravel 4身份验证不适用于子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆