lewis4u
lewis4u

Reputation: 15057

Log out user and change location after certain period of time

I have written the basics only and I don't know what would be the best way to log out the user and redirect him to some other page after let's say 10 minutes.

Simple routes:

Route::get('/post/create', 'PostsController@create');
Route::post('/post/store', 'PostsController@store');

Controller function:

public function create()
{
    return view('form_components_html.create');
}

I know there is this function Auth::logout(); But I don't know how and where to put it, and how to delay it for 10 min?

Upvotes: 0

Views: 355

Answers (1)

Adarsh Sojitra
Adarsh Sojitra

Reputation: 2199

Add the following jQuery to your pages on which you want to do this. I recommend you to do this using jQuery because it's really easy to understand and implement.

Jquery:

var activityTimeout = setTimeout(inActive, 600000);

function resetActive(){
    clearTimeout(activityTimeout);
    activityTimeout = setTimeout(inActive, 600000);
}

function inActive(){
    window.location.replace("{{ url('logout') }}");
}

// Check for mousemove, could add other events here such as checking for key presses ect.
$(document).bind('mousemove', function(){resetActive()});

You can add this code to the layout of your application so that you don't have to add it in every page. This will check if mouse move event happens. If it happens, it will reset the timeout and if it does not, It will redirect a user to logout URL after 10 minutes of last activity. You can specify any URL you want!

According to your question, this might help! let me know what do you think about this.

Upvotes: 1

Related Questions