From acce67f51441c03a2a2eff372e3f069f5b6fc3bf Mon Sep 17 00:00:00 2001 From: Darko Gjorgjijoski Date: Fri, 3 Apr 2026 23:46:07 +0200 Subject: [PATCH] Fix CSRF token mismatch after logout by refreshing cookie After logout invalidates the session, the SPA still holds the old CSRF cookie. Subsequent login attempts succeed but bootstrap/API calls fail with CSRF mismatch, causing redirect back to login. Fix: fetch a fresh CSRF cookie via /sanctum/csrf-cookie after logout completes. --- resources/scripts/admin/stores/auth.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/resources/scripts/admin/stores/auth.js b/resources/scripts/admin/stores/auth.js index 08229eca..53a3f2fc 100644 --- a/resources/scripts/admin/stores/auth.js +++ b/resources/scripts/admin/stores/auth.js @@ -46,20 +46,24 @@ export const useAuthStore = (useWindow = false) => { return new Promise((resolve, reject) => { http .post('/auth/logout') - .then((response) => { + .then(async (response) => { const notificationStore = useNotificationStore() notificationStore.showNotification({ type: 'success', message: 'Logged out successfully.', }) + // Refresh CSRF token so next login works cleanly + await http.get('/sanctum/csrf-cookie').catch(() => {}) + window.router.push('/login') - // resetStore.clearPinia() resolve(response) }) .catch((err) => { handleError(err) - window.router.push('/') + // Still refresh CSRF and redirect on error + http.get('/sanctum/csrf-cookie').catch(() => {}) + window.router.push('/login') reject(err) }) })