Skip to content

Commit

Permalink
fix: signin close button disconnect on auth signature rejection (#1515)
Browse files Browse the repository at this point in the history
* fix: signin close button disconnect on auth signature rejection

* fix: share onClose hook with top-level dialog

* chore: lint fixes

* chore: amend changeset

* chore: amend comment
  • Loading branch information
DanielSinclair authored Sep 29, 2023
1 parent 5b8d821 commit 252f02e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-windows-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rainbow-me/rainbowkit': patch
---

Resolved an issue with the Authentication modal where a user's wallet would remain connected if the modal was dismissed with the Close button rather than explicitly using the Cancel button. This fix ensures that dApps can reliably require a user to complete Authentication before RainbowKit enters a connected state.
13 changes: 11 additions & 2 deletions packages/rainbowkit/src/components/ConnectModal/ConnectModal.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react';
import { useDisconnect } from 'wagmi';
import { useConnectionStatus } from '../../hooks/useConnectionStatus';
import ConnectOptions from '../ConnectOptions/ConnectOptions';
import { Dialog } from '../Dialog/Dialog';
import { DialogContent } from '../Dialog/DialogContent';
import { SignIn } from '../SignIn/SignIn';

export interface ConnectModalProps {
open: boolean;
onClose: () => void;
Expand All @@ -13,6 +15,13 @@ export function ConnectModal({ onClose, open }: ConnectModalProps) {
const titleId = 'rk_connect_title';
const connectionStatus = useConnectionStatus();

// when a user cancels or dismisses the SignIn modal for SIWE, disconnect and call onClose
const { disconnect } = useDisconnect();
const onAuthCancel = React.useCallback(() => {
onClose();
disconnect();
}, [onClose, disconnect]);

if (connectionStatus === 'disconnected') {
return (
<Dialog onClose={onClose} open={open} titleId={titleId}>
Expand All @@ -25,9 +34,9 @@ export function ConnectModal({ onClose, open }: ConnectModalProps) {

if (connectionStatus === 'unauthenticated') {
return (
<Dialog onClose={onClose} open={open} titleId={titleId}>
<Dialog onClose={onAuthCancel} open={open} titleId={titleId}>
<DialogContent bottomSheetOnMobile padding="0">
<SignIn onClose={onClose} />
<SignIn onClose={onAuthCancel} />
</DialogContent>
</Dialog>
);
Expand Down
8 changes: 3 additions & 5 deletions packages/rainbowkit/src/components/SignIn/SignIn.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useCallback, useRef } from 'react';
import { UserRejectedRequestError } from 'viem';
import { useAccount, useDisconnect, useNetwork, useSignMessage } from 'wagmi';
import { useAccount, useNetwork, useSignMessage } from 'wagmi';
import { touchableStyles } from '../../css/touchableStyles';
import { isMobile } from '../../utils/isMobile';
import { AsyncImage } from '../AsyncImage/AsyncImage';
Expand Down Expand Up @@ -50,8 +50,6 @@ export function SignIn({ onClose }: { onClose: () => void }) {
const { address } = useAccount();
const { chain: activeChain } = useNetwork();
const { signMessageAsync } = useSignMessage();
const { disconnect } = useDisconnect();
const cancel = () => disconnect();

const signIn = async () => {
try {
Expand Down Expand Up @@ -213,7 +211,7 @@ export function SignIn({ onClose }: { onClose: () => void }) {
{mobile ? (
<ActionButton
label="Cancel"
onClick={cancel}
onClick={onClose}
size="large"
type="secondary"
/>
Expand All @@ -223,7 +221,7 @@ export function SignIn({ onClose }: { onClose: () => void }) {
borderRadius="full"
className={touchableStyles({ active: 'shrink', hover: 'grow' })}
display="block"
onClick={cancel}
onClick={onClose}
paddingX="10"
paddingY="5"
rel="noreferrer"
Expand Down

2 comments on commit 252f02e

@vercel
Copy link

@vercel vercel bot commented on 252f02e Sep 29, 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 252f02e Sep 29, 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.