Laravel custom TokenMismatchException

Laravel custom TokenMismatchException – My CSRF token is throwing a 500 error (as default) when the token doesn’t match. You can reproduce this if your using a SPA (single page app) such as AngularJS say. Just logout and try logging back in it will throw token mismatch 500 error.

That’s because in my back-end function on logout it clears the session.

Now I want to return a 401 unauthorized from my back-end API and have Angular handle this and do something (probably log the user out, and redirect to /login with a message saying please login again or something like that).

So the back-end catches the exception and returns specifics back to our front-end.


Then the front-end catches this error and displays the custom error.

So to recap on a session token error the user is logged out of both front-end and back-end and asked to log in again. Job done.

Sam Deering is a web developer from England (currently living in Australia). In his spare time he enjoys coding, playing chess, reading and investing.

Leave a Reply

Your email address will not be published. Required fields are marked *