The reason this doesn't work is because Flarum doesn't actually update the $request->getAttribute('actor')
value after logout, so it can't be used to know whether the logout was successful. Whether it should be is a whole question in itself since it's a request attribute, not a response attribute 😅
What you could do is check whether $response instanceof RedirectResponse
. We see RedirectResponse
might be returned if the user is already logged out, or after a successful logout https://github.com/flarum/core/blob/master/src/Forum/Controller/LogOutController.php . Other options would be HtmlResponse
in case the CSRF token was invalid. If a 500 error occurs, an exception would be thrown and bump up the middleware tree to the error handler middleware so it's not of concern.
If you're adding the cookie back on every guest request, maybe it wouldn't hurt to delete the cookie every time on logout attempt, even if in a few situations the logout does end up failing? As long as the logout endpoint itself doesn't end up cached.