Skip to content

Commit

Permalink
Fix reconnect auth bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sofvanh committed Dec 3, 2024
1 parent 7cb42f8 commit def911a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions frontend/src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
return;
}

if (storedToken && socket && !user && !loading) {
if (storedToken && socket) {
setLoading(true);
socket.emit('authenticate', storedToken, (response: any) => {
if (response.success) {
setUser(response.user);
} else {
console.error('Authentication failed');
localStorage.removeItem(TOKEN_STORAGE_KEY);
setUser(null);
}
setLoading(false);
});
}
}, [socket, loading, user]);
}, [socket]);

const signIn = (response: CredentialResponse) => {
localStorage.setItem(TOKEN_STORAGE_KEY, response.credential!);
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/contexts/WebSocketContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi
useEffect(() => {
const socket = getSocket();
socket.connect();

// Handle reconnection authentication
socket.io.on('reconnect', () => {
const storedToken = localStorage.getItem('mindmeld_auth_token');
if (storedToken) {
socket.emit('authenticate', storedToken, (response: any) => {
if (!response.success) {
console.error('Re-authentication failed after reconnect');
localStorage.removeItem('mindmeld_auth_token');
window.location.reload();
}
});
}
});

setSocket(socket);

return () => {
Expand Down

0 comments on commit def911a

Please sign in to comment.