Token Mismatch Exception on Login (Laravel)

后端 未结 6 2075
说谎
说谎 2021-01-14 22:14

I followed this tutorial on creating a registration and login page using Laravel.

Everything works smoothly, the only issue is that I am unable to Login. If I provid

6条回答
  •  不要未来只要你来
    2021-01-14 22:51

    I had this problem on login also. From time to time this exception occurred so I stop and tried to reproduce it. I succeed by doing this:

    First I load the login page.

    Then I deleted the cookies.

    Then, without reloading the login page, I entered username and password and tried to login.

    Because session was deleted (when I deleted the cookies), it was normal that this code was not going to pass and it will throw the TokenMismatchException.

    Route::filter('csrf', function() {
        if ( Session::getToken() != Input::get('_token')) {
            throw new Illuminate\Session\TokenMismatchException;
        }
    });
    

    So, what I've done to solve my problem was to add a redirect to login page with a message to inform the user that the session might expired.

    Route::filter('csrf', function() {
        if ( Session::getToken() != Input::get('_token')) {
            return Redirect::to('/admin/login')->with('warning', 'Your session has expired. Please try logging in again.');
        }
    });
    

    Thus, after page reloading, a new session is created and the problem is solved.

提交回复
热议问题