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.
This commit is contained in:
Darko Gjorgjijoski
2026-04-03 23:46:07 +02:00
parent 8e966965f5
commit acce67f514

View File

@@ -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)
})
})