简体   繁体   English

主域和子域的Laravel身份验证以及子域上的重定向

[英]Laravel Authentication for both primary and sub domain plus redirect on sub domain

Hi I am trying to set a different Authentication for my sub domain, eg if the user is a guest and accessing the domain via sub domain I want to redirect them to the sub domain log in and if a user is accessing from the primary domain page I want to redirect them to the primary domain login page. 嗨,我正在尝试为我的子域设置不同的身份验证,例如,如果用户是访客,并且通过子域访问域,我想将他们重定向到子域登录,并且用户正在从主域页面访问我想将它们重定向到主域登录页面。 Primary domain 主域

Route::group(array('before' => 'auth'), function() {

    Route::get('/profile', array(
        'as' => 'profile-user',
        'uses' => 'ProfileController@user'
     ));

  });

URL: example.com/account 网址:example.com/account

So if someone was to access this without logging in they will be redirected to the login page for the primary domain which by default Laravel does it in the filter BUT this doesn't seem to work for sub domains 因此,如果有人在未登录的情况下访问它,他们将被重定向到主域的登录页面,默认情况下,Laravel在过滤器中执行此操作,但对于子域似乎不起作用

SUB DOMAIN 子域

 Route::group(['domain' => 'dev.example.com'], function() {

    Route::group(array('before' => 'auth'), function() {

    Route::group(array('before' => 'my account'), function() {
    Route::get('/mysub/account', array(
        'as' => 'sub-account',
        'uses' => 'AccountController@subAccount'

        ));
    });
  });
});

URL: dev.example.com/mysub/account 网址: dev.example.com/mysub/account

so if someone was to access the above URL without logging in they should be redirected to dev.example.com which is the home page for my sub domain 因此,如果有人要在不登录的情况下访问上述URL,则应将其重定向到dev.example.com ,这是我的子域的主页

The "auth" filter doesn't work for subdomains do in the config/session.php i have tried 'domain' => '.example.com', and ' domain' => '*example.com', but that doesn't seem to do the job plus it completely stopped loggin in from the primary domain “ auth”过滤器不适用于config / session.php中的子域,我尝试了“ domain” =>“。example.com”和“ domain” =>“ * example.com”,但是没有似乎无法完成工作,而且它完全停止了从主域的登录

For the redirect if the Authentication failed from the sub domain, I tried 对于重定向,如果认证从子域失败,我尝试

Route::filter('auth', function()
{
    if (Auth::guest())
    {
        if (Request::ajax())
        {
            return Response::make('Unauthorized', 401);
        }
        elseif (Request::getHost() == 'dev.example.com') {

            return Redirect::guest(URL::route('subdomain-login'));
        }

        else
        {
            return Redirect::guest(URL::route('primary-domain-login'));
        }
    }
});

but this seems to redirect them back "subdomain-login" even if the log in is correct. 但这似乎将它们重定向回“ subdomain-login”,即使登录正确也是如此。

So I found that the best solution for this was to create another auth filter for the sub domain. 因此,我发现最好的解决方案是为子域创建另一个身份验证过滤器。

Route::filter('subauth', function()
{
    if (Auth::guest())
    {
        if (Request::ajax())
        {
            return Response::make('Unauthorized', 401);
        }

        else
        {
            return Redirect::guest(URL::route('login'));
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM