Question

I do not want to show login page after login in laravel 4. If a logged in user want to visit login page it should redirect to homepage('/'). I am using Sentry for authentication.

filter.php

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

routes.php

Route::get('login', array('as' => 'login', function() {
return View::make('login');
}))->before('guest');

Route::post('login', 'AuthController@postLogin');

AuthController.php

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

        // Try to authenticate the user
        $user = Sentry::authenticate($credentials, false);
        if ($user) {
            return Redirect::to('/');
        }
    } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
        return Redirect::to('login')->withErrors('Login field is required');
    }
}

After successful login if, if login page is requested it still shows the login page

Was it helpful?

Solution

If you're using Laravel's default guest filter, it will not work, because the default guest filter does not check if your Sentry user is logged in.

Try this instead:

Route::filter(
    'filter', function () {
    if (Sentry::check()) {
        return Redirect::to('/');
    }
}

In your routes.php, the filter is already being applied to the login route, so things should work this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top