{"id":1575,"date":"2024-09-10T17:30:24","date_gmt":"2024-09-10T17:30:24","guid":{"rendered":"https:\/\/debugspot.com\/?p=1575"},"modified":"2024-09-15T17:04:45","modified_gmt":"2024-09-15T17:04:45","slug":"how-to-avoid-tokenmismatch-error-laravel","status":"publish","type":"post","link":"https:\/\/debugspot.com\/blogs\/how-to-avoid-tokenmismatch-error-laravel\/","title":{"rendered":"How to avoid TokenMismatchException on logout"},"content":{"rendered":"<p>Learn how to fix the TokenMismatchException error and handle CSRF protection issues in Laravel effectively. Follow our step-by-step guide to resolve the Laravel logout CSRF token problem and ensure a smooth logout experience.<\/p>\n<p>Many time we faced TokenMismatchException error in laravel, this error occurred If you stay too long time on one form or if your system stay on idle or you are not active on your computer, and then again try to fill this form.<\/p>\n<p>At that time you may get a TokenMismatchException error, because the CSRF token won\u2019t be the same. recently many time we found this problems in logout time. So, In this example i will show you to how to avoid it.<\/p>\n<p>Normally, if you are not active for long time in your system then you will get this error.<\/p>\n<p>To avoid TokenMismatchException error, we may add exceptions for the URLs that we don\u2019t want to have CSRF protection. There are special array for that in app\/Http\/Middleware\/VerifyCsrfToken.php<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\">\r\n class VerifyCsrfToken extends Middleware\r\n  {\r\n      \/**\r\n        * The URIs that should be excluded from CSRF verification.\r\n        *\r\n        * @var array\r\n        *\/\r\n      protected $except = [\r\n          \/\/\r\n      ];\r\n  }  \r\n <\/code><\/pre>\n<p>So what we need do, just add logout into this array<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> \r\n  protected $except = [\r\n  '\/logout'\r\n    ]; \r\n<\/code><\/pre>\n<p>If you want to add more URLs then you can add here, but  CSRF protection is also important.  <\/p>\n<p>For more detail you can write <a href=\"https:\/\/laravel.com\/docs\/11.x\/csrf\" target=\"_blank\" rel=\"noopener\">Laravel Documentation<\/a><\/p>\n<h5>You may also find interesting:<\/h5>\n<p><a title=\"Laravel Scout in Laravel 11: A Comprehensive Tutorial with Example\" href=\"https:\/\/debugspot.com\/blogs\/log-out-users-from-other-devices-in-laravel-11\/\" target=\"_blank\" rel=\"noopener\">Effortlessly Log Out Users from Other Devices in Laravel 11 \u2013 Ultimate Guide<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to fix the TokenMismatchException error and handle CSRF protection issues in Laravel effectively. Follow our step-by-step guide to resolve the Laravel logout CSRF token problem and ensure a smooth logout experience. Many time we faced TokenMismatchException error in laravel, this error occurred If you stay too long time on one form or if your system stay on idle or you are not active on your computer, and then again try to fill this form. At that time you may get a TokenMismatchException error, because the CSRF token won\u2019t be the same. recently many time we found this problems in logout time. So, In this example i will show you to how to avoid it. Normally, if you are not active for long time in your system then you will get this error. To avoid TokenMismatchException error, we may add exceptions for the URLs that we don\u2019t want to have CSRF protection. There are special array for that in app\/Http\/Middleware\/VerifyCsrfToken.php class VerifyCsrfToken extends Middleware { \/** * The URIs that should be excluded from CSRF verification. * * @var array *\/ protected $except = [ \/\/ ]; } So what we need do, just add logout into this array &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"rank_math_lock_modified_date":false,"footnotes":""},"categories":[3],"tags":[46,53],"class_list":["post-1575","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-laravel","tag-laravel11"],"acf":[],"_links":{"self":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1575"}],"collection":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/comments?post=1575"}],"version-history":[{"count":5,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1575\/revisions"}],"predecessor-version":[{"id":1580,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1575\/revisions\/1580"}],"wp:attachment":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/media?parent=1575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/categories?post=1575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/tags?post=1575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}