Skip to content

Commit

Permalink
fix: sign out if wallet is switched (#1553)
Browse files Browse the repository at this point in the history
* 🧼 sign out if wallet is switched

* ✨ npx changesets

* quick comment

* quick comment fix

* 🧼 import types from wagmi only

* chore: changeset tweak

---------

Co-authored-by: Daniel Sinclair <[email protected]>
  • Loading branch information
magiziz and DanielSinclair authored Oct 11, 2023
1 parent cd5abee commit 7d97860
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-maps-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rainbow-me/rainbowkit": patch
---

Fixed an issue where a user would not get automatically logged out from the Authentication API after switching their wallet in MetaMask or other browser wallets. Users must now sign a new SIWE message after switching wallets.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, {
useMemo,
useRef,
} from 'react';
import { useAccount } from 'wagmi';
import { ConnectorData, useAccount } from 'wagmi';

export type AuthenticationStatus =
| 'loading'
Expand Down Expand Up @@ -55,7 +55,7 @@ export function RainbowKitAuthenticationProvider<Message = unknown>({
}: RainbowKitAuthenticationProviderProps<Message>) {
// When the wallet is disconnected, we want to tell the auth
// adapter that the user session is no longer active.
useAccount({
const { connector } = useAccount({
onDisconnect: () => {
adapter.signOut();
},
Expand All @@ -77,6 +77,27 @@ export function RainbowKitAuthenticationProvider<Message = unknown>({
}
}, [status, adapter, isDisconnected]);

const handleChangedAccount = ({ account }: ConnectorData) => {
// Only if account is changed then signOut
if (account) adapter.signOut();
};

// Wait for user authentication before listening to "change" event.
// Avoid listening immediately after wallet connection due to potential SIWE authentication delay.
// Ensure to turn off the "change" event listener for cleanup.
// biome-ignore lint/nursery/useExhaustiveDependencies: <explanation>
useEffect(() => {
if (connector && status === 'authenticated') {
// Attach the event listener when status is 'authenticated'
connector.on('change', handleChangedAccount);

// Cleanup function to remove the event listener
return () => {
connector?.off('change', handleChangedAccount);
};
}
}, [connector, status]);

return (
<AuthenticationContext.Provider
value={useMemo(
Expand Down

2 comments on commit 7d97860

@vercel
Copy link

@vercel vercel bot commented on 7d97860 Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 7d97860 Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.