Reputation: 405
I'm trying to change the expiration time of the remember me cookie when the user checks the option and logs in, but the expiration time doesn't change when I run this code. What do I need to do to fix this in Laravel 4?
More specifically what I want to do is set the expiration time to 12 hours. I tried the App:after method but every time that code runs, it refreshes the existing cookie back to the original 12 hours. How would I make it run so that it doesn't do that?
LoginController.php
class LoginController extends BaseController {
public function __construct()
{
$this->beforeFilter('csrf', array('on' => 'post'));
}
public function getIndex()
{
if (Auth::check()) {
return Redirect::action('HomeController@usersIndex')
->with('message', 'You are already logged in.');
}
return View::make('guests.login');
}
public function postIndex()
{
$validation = User::validateForm(Input::all());
if ($validation->passes()) {
$user = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
// Try to log the user in.
if (Auth::attempt($user, Input::has('remember_me'))) {
return Redirect::to('/');
}
return Redirect::to('login')
->withErrors(array('password' => 'The username or password you entered is incorrect.'))
->withInput(Input::except('password'));
}
return Redirect::to('login')
->withErrors($validation)
->withInput(Input::except('password'));
}
}
filters.php
App::after(function($request, $response) {
if ( Auth::check()){
$ckname=Auth::getRecallerName(); //Get the name of the cookie, where remember me expiration time is stored
$ckval=Cookie::get($ckname); //Get the value of the cookie
return $response->withCookie(Cookie::make($ckname,$ckval,720)); //change the expiration time
}
});
routes.php
Route::get('/', 'HomeController@usersIndex');
Route::get('logout', 'HomeController@logout');
Route::controller('login', 'LoginController');
Route::controller('register', 'RegistrationController');
Route::controller('password', 'PasswordController');
Upvotes: 0
Views: 5049
Reputation: 3668
You can use App::after
method to change rememberme cookie expiration time.
so in your filters.php
file find App::after()
method and change the cookie expiration time. For example-
App::after(function($request, $response)
{
if ( Auth::check()){
$ckname=Auth::getRecallerName(); //Get the name of the cookie, where remember me expiration time is stored
$ckval=Cookie::get($ckname); //Get the value of the cookie
return $response->withCookie(Cookie::make($ckname,$ckval,360)); //change the expiration time
}
});
From Discussion:
You: @Trying What I want to do is set the expiration time to 12 hours. I tried the App:after
method but every time that code runs, It overwrites/refreshes back to the original 12 hours. How would I fix this?
Me: You can try setting session value in ur login method and thn check it in App::after
method, the session value,
If it exists update the cookie and delete the session value,
If u do this it will only update the cookie once i,e when u login.
Example:-
Login code
if (Auth::attempt($user, Input::has('remember_me'))) {
Session::put('key',value);
return Redirect::to('/');
}
App after method
App::after(function($request, $response) {
if(Session::has('key')){
if ( Auth::check()){
$ckname=Auth::getRecallerName(); //Get the name of the cookie, where remember me expiration time is stored
$ckval=Cookie::get($ckname); //Get the value of the cookie
Session::forget('key');
return $response->withCookie(Cookie::make($ckname,$ckval,720)); //change the expiration time
}
}
});
Upvotes: 1