首页 文章

laravel sentry redirect ::打算不工作

提问于
浏览
7

我正在尝试正确地在我的应用程序中设置哨兵包 .

我可以登录和退出用户并保护路线,但我似乎无法让 redirect::intended 正常工作 . 我的理解是,在被定向到登录页面之前,用户将被带回他们最初呼叫的路由 . 目前它只是保持重定向到默认页面 .

在我的routes.php中,我设置了以下组:

Route::group(array('before' => 'sentryAuth'), function () {...}

在这个小组中,我放置了所有受保护的路线 .

在我的filters.php中,我有以下过滤器:

Route::filter('sentryAuth', function () {

if (!Sentry::check()) {

    return Redirect::route('login');
} 
});

Route :: filter('sentryGuest',function(){

if (Sentry::check()) {
    return Redirect::intended('dashboard');
}
});

在我的userController中,我有以下代码:

public function postAuthenticate()
{
    try {
        // Set login credentials
        $credentials = array(
            'email' => Input::get('email'),
            'password' => Input::get('password')
        );

        // Try to authenticate the user
        $user = Sentry::authenticate($credentials, false);
    } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
        echo 'Login field is required.';
    }
    catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) {
        echo 'Password field is required.';
    }
    catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
        echo 'User was not found.';
    }
    catch (Cartalyst\Sentry\Users\WrongPasswordException $e) {
        echo 'Wrong password, try again.';
    }
    catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) {
        echo 'User is not activated.';
    }

    if (!Sentry::check()) {
        return Redirect::to('user/login');
    } else {
        return Redirect::intended('dashboard');
    }
}

我试图在没有登录的情况下访问页面'预订/创建' . 我被带到登录页面,登录但是它带我到仪表板而不是预订/创建 .

我错过了什么吗?我需要额外的代码才能使其工作吗?

4 回答

  • 0

    我不确定这一点,因为我没有将Sentry用于我当前的项目..所以这只是一种预感 .

    似乎既然你在laravel 4中使用了SentryAuth而不是原生的Auth类,那么预设网址的会话项就没有设置..我查看了Redirector类的API,我看到了这个:

    public function intended($default, $status = 302, $headers = array(), $secure = null)
    {
        $path = $this->session->get('url.intended', $default);
    
        $this->session->forget('url.intended');
    
        return $this->to($path, $status, $headers, $secure);
    }
    

    并且如代码所示,会话密钥是'url.intended' . 我使用原生Auth过滤器对此进行了验证,并且预期的URL在 Session::get('url.intended') 上按预期设置 .

    所以可能的解决方案是在会话中手动设置它 . 一个例子是:

    On your filter

    Route::filter('sentryAuth', function () {
    
        if (!Sentry::check()) {
            Session::put('loginRedirect', Request::url());
            return Redirect::route('login');
        } 
    });
    

    On your postAuthenticate() method

    if (!Sentry::check()) {
        return Redirect::to('user/login');
    } else {
        // Get the page we were before
        $redirect = Session::get('loginRedirect', 'dashboard');
    
        // Unset the page we were before from the session
        Session::forget('loginRedirect');
    
        return Redirect::to($redirect);
    }
    

    部分代码取自here以供参考.. ^ _ ^

  • 8

    在您的filters.php中,请务必使用:

    return Redirect::guest('login');
    

    代替

    return Redirect::route('login');
    

    guest函数将为expected()设置正确的会话变量以使其正常工作 .

  • 10

    @ reikyoushin的答案很棒 . 这是一个略有不同的版本 .

    routes.php

    // NOTE: do NOT name your filter "auth" as it will not override
    //       Laravel's built-in auth filter and will not get executed
    Route::filter( 'sentryAuth', function()
    {
        // check if logged in or not
        if ( ! Sentry::check() )
        {
            // the guest() method saves the intended URL in Session
            return Redirect::guest( 'user/login' );
        } else {
            // now check permissions for the given route
            $user = Sentry::getUser();
            if ( ! $user->hasAccess( Route::currentRouteName() ) )
            {
                // redirect to 403 page
                return Response::make( 'Forbidden', 403 );
            }
        }
    });
    
    // Protected routes
    Route::group( array( 'before' => 'sentryAuth', function()
    {
        Route::get( 'admin', function() { return View::make( 'admin.index' ); } );
    });
    

    并在您的登录功能:

    public function login() {
        try {
            $creds = array(
                'email'    => Input::get( 'email' ),
                'password' => Input::get( 'password' )
            );
            $user = Sentry::authenticate( $creds );
            return Redirect::intended( 'dashboard' );
        } catch ( Exception $e ) {
            // handle exceptions
        }
    }
    
  • 4

    最简单的方法 .

    在您的Auth过滤器中使用以下内容:

    Route::filter('sentryAuth', function()
    {
        if ( ! Sentry::check()) 
        {     
            return Redirect::guest(route('login'));
        }
    });
    

    In your controller:

    public function postAuthenticate()
    {
        try {
              $credentials = array(
                'email' => Input::get('email'),
                'password' => Input::get('password')
              );
    
             $user = Sentry::authenticate($credentials, false);
         } catch () {
            // validation errors
         }
    
        //firstly, Laravel will try to redirect intended page. 
        //if nothing saved in session, it will redirect to home. 
        //you can use any route instead of home.
    
         return Redirect::intended(route('home'));
    }
    

    完成 .

    Outside of Sentry:

    此代码可以与任何类型的身份验证库一起使用 . 您需要两件事来实现预期的重定向:

    1.在您的身份验证过滤器中:

    Route::filter('auth', function()
        {
          if ( ! Sentry::check()) 
              {
                return Redirect::guest(route('login'));
              }
        });
    

    2.登录用户后:

    //firstly, Laravel will try to redirect intended page. 
    //if nothing saved in session, it will redirect to home. 
    //you can use any url instead of '/'
    
    return Redirect::intended('/');
    

相关问题