Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the filter does not work when the back button of the browser is pressed in laravel?

I have a view to enter an account, then called the controller where the data is validated and are then saved with the method of authentication

    public function doLogin(){
    $rules = array(
                    'email' => 'required|email',
                    'password' => 'required'
                    );

    $validator = Validator::make(Input::all(), $rules);
    //dd(Input::all());

    if($validator->fails()){
        return Redirect::to('usuarios')->withErrors($validator)->withInput(Input::except('password'));
    }else{

        $userdata = array(
            'email' => Input::get('email'),
            'password' => Input::get('password')
            );

        if(Auth::attempt($userdata)){
            return View::make('principal');
        }else{

            return Redirect::to('usuarios');
        }

    }
}

I also have the function to exit the session

Route::get('usuarios/logout', function(){
        Auth::logout();
        return Redirect::to('usuarios'); //login page
})->before('auth');

The problem is that when I press the back button of the browser, I can use without problems the application but without the authentication.

Route

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

Route::get('usuarios/view', function(){
    $usuarios = Usuario::paginate(5);
    return View::make('viewusuario', array('usuarios' => $usuarios));
})->before('auth');

Route::get('usuario/create', function(){
    return View::make('formusuario');
})->before('auth');

Filter

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::guest('usuarios'); //login page
});
Route::filter('guest', function()
{
    if (Auth::check()){
        return View::make('principal'); //home page     
    }
});

How can I fix it?

like image 637
TuGordoBello Avatar asked May 28 '14 15:05

TuGordoBello


People also ask

How to prevent back button after login?

Here's an easy and quick solution. To the login form tag add target="_blank" which displays content in a different window. Then after logout simply close that window and the back button problem (Safari browser) is solved. Even trying to use the history will not display the page and instead redirect to login page.


1 Answers

The problem is related to browser cache, not Laravel.

To handle browser cache you can use the following code in one of your start files or in a service provider:

App::after(function($request, $response)
{
    $response->headers->set('Cache-Control','nocache, no-store, max-age=0, must-revalidate');
    $response->headers->set('Pragma','no-cache');
    $response->headers->set('Expires','Fri, 01 Jan 1990 00:00:00 GMT');
});

Here we are just modifying each responses within Laravel using application events.


Some people said that this works for all web browser but not for IE. So for IE, you should add a bunch of meta tags to your layout:

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="cache-control" content="no-store" />
<meta http-equiv="cache-control" content="must-revalidate" />
<meta http-equiv="expires"       content="0" />
<meta http-equiv="expires"       content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma"        content="no-cache" />
like image 176
Rubens Mariuzzo Avatar answered Oct 22 '22 19:10

Rubens Mariuzzo