I'm sorry for the long title. Although I've seen similar posts, this problem is specific.
I use Chrome for testing my projects. Seemingly out of nowhere I started getting TokenMismatch Exceptions when I submit forms. I knew this is could be because the CSRF token expires after some time. However this happens consistently, even if you submit the form immediately after the page loads. I eventually figured out the Exception only occurs when I checked "Remember Me" at login. Here are my test results:
-
Chrome + Uncheck Remember Me at login + Form Post = Success
-
Chrome + Check Remember Me at login + Form Post = TokenMismatch Exception
-
Chrome + Check Remember Me at login + Delete Remember Me Cookie + Form Post = Success
-
Firefox/IE + Check/Uncheck Remember Me at login + Form Post = Success
I figured out the reason why I'm getting TokenMismatch Exception. In between the form post I mysteriously lose the session. A new session generates automatically in the background, resulting in a new CSFR token, resulting in the TokenMismatch Exception.
I tried everything I could think of:
- deleting cookies/browser data
- restarting Chrome
- reinstalling Chrome
- restarting the server
- changing the Laravel session driver
- removing middleware
- reinstalling Laravel
- deleting the vendor folder
- starting a fresh Laravel project
Absolutely none of these fix or change the problem. I've been at this for 2 days now, scouring Google for clues. I can't find any answers, only a few posts from people who may have had the same problem but haven't yet reported an answer. Very strange.
Finally, after a night's sleep I found my biggest clue yet. I setup a temporary dd($request) to reveal the request variables. When the TokenMismatch Exception occurs, within the response output I get:
Request
{+cookies: ParameterBag {
#parameters: array[
larvel_session: null
Note the "null" for larvel_session. When the form posts successfully (on Firefox with Remember Me cookie), the dd($request) outputs:
Request
{+cookies: ParameterBag {
#parameters: array:[
larvel_session: session_token_here
I checked the post headers in Chrome. Everything looks correct. Chrome is sending the session cookie to the server. I put a temporary var_dump($_COOKIE) at the top of the server.php file. Chrome is sending the cookie to PHP. However, the dd($request) shows me that Lavavel drops the ball somewhere, changing the value to null. Maybe there is some logic as to why it's doing this. Still, I need to fix it so the session persists and the form posts without a TokenMismatch Exception. The "session = null" is a great clue, but I don't nearly know enough about Laravel to troubleshoot past this point. What might be going on behind the scenes to cause Laravel to "null" the incoming session cookie when receiving a form post from Chrome along with the remember me cookie. It's a mouthful, but a valid description.
If you have experienced this phenomenon, please share the steps you took to repair it. Also, if you have any suggestions on how to solve this problem I would truly appreciate it, as will the next person frantically Googling for clues. I've come so far learning Laravel, I'd hate to have to stop here. Thanks!