2

I want to take user automatically after some alert to the login page because of inactivity. To do that I am submitting logout form using Javascript. Logout Form is generated automatically with other pages(Login, Register etc) after running make:auth command. Even after submitting the same form by using Javascript I am getting "The page has expired due to inactivity" error. Following is form and Javascript code.

Form and Its automatic generated Javascript Code:

<a id="lnkLogout" href="{{ route('logout') }}" 
onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
Logout</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
 {{ csrf_field() }}
</form>

My Code:

<script type="text/javascript" language="javascript">
    var idleMax = 1; // Logout after 10 minutes of IDLE
    var idleTime = 0;

    var idleInterval = setInterval("timerIncrement()", 60000);  // 1 minute interval
    $( "body" ).mousemove(function( event ) {
        idleTime = 0; // reset to zero
    });

    // count minutes
    function timerIncrement() {
        idleTime = idleTime + 1;
        if (idleTime > idleMax) {
            $("#lnkLogout").click();
        }
    }
</script>

Route for Logout:

Route::post('logout', 'Auth\LoginController@logout')->name('logout');

Function being used to logout user from AuthenticatesUsers trait:

public function logout(Request $request)
{
    $this->guard()->logout();

    $request->session()->invalidate();

    return redirect('/');
}
aishazafar
  • 1,024
  • 3
  • 15
  • 35
  • Look this one might be useful for you https://stackoverflow.com/questions/46149561/laravel-5-5-the-page-has-expired-due-to-inactivity-please-refresh-and-try-again?answertab=active#tab-top – Ayaz Ali Shah Nov 12 '18 at 10:40
  • @AyazShah thank you so much for the help but initially I also thought its a CSRF issue but if you see my code my form is posting this data. – aishazafar Nov 12 '18 at 10:42
  • Post your controller method – Paras Nov 12 '18 at 11:55
  • @Paras its not controller function issue. But I am updating my post so that you better understand my issue. – aishazafar Nov 12 '18 at 12:21
  • @Paras You can have a look now. – aishazafar Nov 12 '18 at 12:27
  • As soon as you logout your csrf token will change, and will render the form invalid. Is it possible that you might be submitting two `logout` requests? – Adam Rodriguez Nov 12 '18 at 16:16
  • I am calling same function which is being used on click event of logout link. How this is possible that it works fine with click event but If I submit form automatically using javascript then it does not work. Moreover there is only one form how two logouts will be submitting? – aishazafar Nov 12 '18 at 16:47

0 Answers0