Skip to content

Commit

Permalink
[Enhancement #585] Mark the current websocket security token as inval…
Browse files Browse the repository at this point in the history
…id instead of clearing it to prevent looping
  • Loading branch information
lukaskabc authored and ledsoft committed Dec 10, 2024
1 parent 8ef6d51 commit 617b291
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/WebSocketApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,28 @@ export const WebSocketWrapper: React.FC<{
loadDispatchers = true,
}) => {
const [securityToken, setSecurityToken] = useState<string>("");
const [isTokenInvalid, setIsTokenInvalid] = useState<boolean>(false);

useEffect(() => {
const callback = () => {
const token = SecurityUtils.loadToken() || "";
// using length prevents from aborting websocket due to token refresh
// but will abort it when token is cleared or new one is set
if (token.length !== securityToken.length) {
if (
token.length !== securityToken.length ||
(isTokenInvalid && token !== securityToken)
) {
setSecurityToken(token);
setIsTokenInvalid(false);
}
};
callback();
return BrowserStorage.onTokenChange(callback);
}, [securityToken]);
}, [securityToken, isTokenInvalid]);

return (
<Provider
enabled={securityToken.trim() !== ""}
enabled={securityToken.trim() !== "" && !isTokenInvalid}
url={Constants.WEBSOCKET_URL}
connectHeaders={{
Authorization: securityToken,
Expand All @@ -121,7 +126,7 @@ export const WebSocketWrapper: React.FC<{
}
onStompError={(frame) => {
console.error("WebSocket STOMP error", frame);
setSecurityToken("");
setIsTokenInvalid(true);
}}
>
{loadDispatchers && registerDispatchers()}
Expand Down

0 comments on commit 617b291

Please sign in to comment.