简体   繁体   中英

Token mismatch exception only on every first attempt laravel 5.1

Every time I log in to my application for the first time after an hour or so or on every new device I get token mismatch exception. But when I try again right after the problem goes away. I am using 755 on storage/framework/sessions -- I have the same problem on my local vagrant scotch box 1.5 box as well as onmy Digital Ocean LAMP. Any ideas?

The problem is that the CSRF Token has expired and your browser need a new token to make POST request. The expiration time by default in Laravel is 2 hours.

I have the same problem and I am trying this solution https://laracasts.com/discuss/channels/general-discussion/crsf-checked-before-auth

UPDATE:

You need to update the render method in app/Exceptions/Handler.php and handle the Exception: TokenMismatchException

Code Example

   /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if ($e instanceof \Illuminate\Session\TokenMismatchException) {
            return redirect('/')->with('message', 'Sorry, your session seems to have expired. Please login again.');
        }

        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }

        return parent::render($request, $e);
    }

I can't but tell you that if you're coding for a Laravel project you should give homestead box a try, I'm using it for a year now and no weird issues have happened and my logins work flawlessly out of the box.

http://laravel.com/docs/5.1/homestead

Maybe this can help you out quicker than figuring out the issue on your current box :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM