This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 926
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A
useSignIn()
hook for accessing the Sign In With Solana feature
- Loading branch information
1 parent
a3cb9fa
commit 46ad8e6
Showing
9 changed files
with
547 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
'@solana/react': minor | ||
--- | ||
|
||
Added a `useSignIn` hook that, given a `UiWallet` or `UiWalletAccount`, returns a function that you can call to trigger a wallet's [‘Sign In With Solana’](https://phantom.app/learn/developers/sign-in-with-solana) feature. | ||
|
||
#### Example | ||
|
||
```tsx | ||
import { useSignIn } from '@solana/react'; | ||
|
||
function SignInButton({ wallet }) { | ||
const csrfToken = useCsrfToken(); | ||
const signIn = useSignIn(wallet); | ||
return ( | ||
<button | ||
onClick={async () => { | ||
try { | ||
const { account, signedMessage, signature } = await signIn({ | ||
domain: window.location.host, | ||
nonce: csrfToken, | ||
}); | ||
// Inspect the contents of `signedMessage` and verify it against `signature` | ||
window.alert(`You are now signed in with the address ${account.address}`); | ||
} catch (e) { | ||
console.error('Failed to sign in', e); | ||
} | ||
}} | ||
> | ||
Sign In | ||
</button> | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { ExclamationTriangleIcon } from '@radix-ui/react-icons'; | ||
import { Button, Callout, DropdownMenu } from '@radix-ui/themes'; | ||
import { SolanaSignIn } from '@solana/wallet-standard-features'; | ||
import type { UiWallet } from '@wallet-standard/react'; | ||
import { useWallets } from '@wallet-standard/react'; | ||
import { useContext, useRef, useState, useTransition } from 'react'; | ||
|
||
import { SelectedWalletAccountContext } from '../context/SelectedWalletAccountContext'; | ||
import { ErrorDialog } from './ErrorDialog'; | ||
import { SignInMenuItem } from './SignInMenuItem'; | ||
import { ErrorBoundary } from 'react-error-boundary'; | ||
import { UnconnectableWalletMenuItem } from './UnconnectableWalletMenuItem'; | ||
|
||
type Props = Readonly<{ | ||
children: React.ReactNode; | ||
}>; | ||
|
||
export function SignInMenu({ children }: Props) { | ||
const { current: NO_ERROR } = useRef(Symbol()); | ||
const wallets = useWallets(); | ||
const [_, setSelectedWalletAccount] = useContext(SelectedWalletAccountContext); | ||
const [error, setError] = useState<unknown | typeof NO_ERROR>(NO_ERROR); | ||
const [forceClose, setForceClose] = useState(false); | ||
const [_isPending, startTransition] = useTransition(); | ||
function renderItem(wallet: UiWallet) { | ||
return ( | ||
<ErrorBoundary | ||
fallbackRender={({ error }) => <UnconnectableWalletMenuItem error={error} wallet={wallet} />} | ||
key={`wallet:${wallet.name}`} | ||
> | ||
<SignInMenuItem | ||
onSignIn={account => { | ||
startTransition(() => { | ||
setSelectedWalletAccount(account); | ||
setForceClose(true); | ||
}); | ||
}} | ||
onError={setError} | ||
wallet={wallet} | ||
/> | ||
</ErrorBoundary> | ||
); | ||
} | ||
const walletsThatSupportSignInWithSolana = []; | ||
for (const wallet of wallets) { | ||
if (wallet.features.includes(SolanaSignIn)) { | ||
walletsThatSupportSignInWithSolana.push(wallet); | ||
} | ||
} | ||
return ( | ||
<> | ||
<DropdownMenu.Root open={forceClose ? false : undefined} onOpenChange={setForceClose.bind(null, false)}> | ||
<DropdownMenu.Trigger> | ||
<Button> | ||
{children} | ||
<DropdownMenu.TriggerIcon /> | ||
</Button> | ||
</DropdownMenu.Trigger> | ||
<DropdownMenu.Content> | ||
{walletsThatSupportSignInWithSolana.length === 0 ? ( | ||
<Callout.Root color="orange" highContrast> | ||
<Callout.Icon> | ||
<ExclamationTriangleIcon /> | ||
</Callout.Icon> | ||
<Callout.Text> | ||
This browser has no wallets installed that support{' '} | ||
<a href="https://phantom.app/learn/developers/sign-in-with-solana" target="_blank"> | ||
Sign In With Solana | ||
</a> | ||
. | ||
</Callout.Text> | ||
</Callout.Root> | ||
) : ( | ||
walletsThatSupportSignInWithSolana.map(renderItem) | ||
)} | ||
</DropdownMenu.Content> | ||
</DropdownMenu.Root> | ||
{error !== NO_ERROR ? <ErrorDialog error={error} onClose={() => setError(NO_ERROR)} /> : null} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { DropdownMenu } from '@radix-ui/themes'; | ||
import { useSignIn } from '@solana/react'; | ||
import type { UiWallet, UiWalletAccount } from '@wallet-standard/react'; | ||
import React, { useCallback, useState } from 'react'; | ||
|
||
import { WalletMenuItemContent } from './WalletMenuItemContent'; | ||
|
||
type Props = Readonly<{ | ||
onError(err: unknown): void; | ||
onSignIn(account: UiWalletAccount | undefined): void; | ||
wallet: UiWallet; | ||
}>; | ||
|
||
export function SignInMenuItem({ onSignIn, onError, wallet }: Props) { | ||
const signIn = useSignIn(wallet); | ||
const [isSigningIn, setIsSigningIn] = useState(false); | ||
const handleSignInClick = useCallback( | ||
async (e: React.MouseEvent) => { | ||
e.preventDefault(); | ||
try { | ||
setIsSigningIn(true); | ||
try { | ||
const { account } = await signIn({ | ||
domain: window.location.host, | ||
statement: 'You will enjoy being signed in.', | ||
}); | ||
onSignIn(account); | ||
} finally { | ||
setIsSigningIn(false); | ||
} | ||
} catch (e) { | ||
onError(e); | ||
} | ||
}, | ||
[signIn, onSignIn, onError], | ||
); | ||
return ( | ||
<DropdownMenu.Item onClick={handleSignInClick}> | ||
<WalletMenuItemContent loading={isSigningIn} wallet={wallet} /> | ||
</DropdownMenu.Item> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.