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

I have an idea of โ€‹โ€‹logging into the account, then the controller is called, where the data is verified and then saved using the authentication method.

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 a logout function

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

The problem is that when I click the back button of the browser, I can use the application without problems, but without 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 this?

+6
source share
2 answers

The problem is with the browser cache, not Laravel.

To process the browser cache, you can use the following code in one of your startup files or from your 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 simply modify each response in Laravel using application events .


Some people said that this works for the entire web browser, but not for IE. So, for IE you have to 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" /> 
+6
source

The problem is the browser cache, when you go back, the browser does not ask for everything, for the sake of speed, it just loads the page from the cache. But when you reload this ctrl + r page, it will not work. You can turn off page caching, as other people say, but it will cost you website performance and additional bills if you are on a cloud hosting. So the cache is good, hold it.

You should try to group all the pages that need the auth filter so that you don't forget to add this filter, we are all people, sometimes we forget.

 Route::group(['before' => 'auth', function(){ Route::get('usarios/view', function() { ... }); Route::get('usarios/create', function() { ... }); }]); Route::group(['before' => 'guest', function(){ Route::get('usarios/login', ' AuthController@showLogin '); Route::post('usarios/login', ' AuthController@doLogin '); }]); 

Also, try not to display the main user page in the login post , in your case doLogin . You must redirect the user to a new page with the user's home page.

0
source

Source: https://habr.com/ru/post/969987/


All Articles