Laravel sentry redirect::intended not working

authenticationcartalyst-sentrylaravel

I'm trying to get the sentry package set up in my app correctly.

I can log a user in and out and protect routes but I can't seem to get the redirect::intended to work properly. My understanding is that a user will be taken back to the route they originally called before being directed to the login page. At the moment it simply keeps redirecting to the default page.

In my routes.php I have the following group set up:

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

Within this group I've placed all the protected routes.

In my filters.php I have the following filters:

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

if (!Sentry::check()) {

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

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

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

In my userController I have the following code:

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

I've tried to access a page 'bookings/create' without being logged in. I get taken to the login page, log in but it then takes me to the dashboard not to bookings/create.

AM I missing something here? Is there additional code I need to get the intended to work??

Best Answer

In your filters.php make sure to use:

 return Redirect::guest('login');

instead of

 return Redirect::route('login');

The guest function will set the correct session variables for intended() to work properly.

Related Topic