useConnectWithSiwe
Connect to Openfort using Sign-In with Ethereum (SIWE) after a wallet has been connected via wagmi. This hook is used internally by useWalletAuth but can be used directly for custom wallet connection flows.
Usage
import { useConnect } from 'wagmi';
import { useConnectWithSiwe } from '@openfort/react';
function CustomWalletConnect() {
const { connect, connectors } = useConnect();
const connectWithSiwe = useConnectWithSiwe();
const handleConnect = async () => {
// First connect the wallet using wagmi
await connect({ connector: connectors[0] });
// Then authenticate with SIWE
await connectWithSiwe({
onConnect: () => console.log('Connected!'),
onError: (message) => console.error('Error:', message),
});
};
return <button onClick={handleConnect}>Connect Wallet</button>;
}Return type
The hook returns a function that initiates the SIWE authentication flow.
type ConnectWithSiwe = (options?: SiweConnectOptions) => Promise<void>
type SiweConnectOptions = {
// Override the connector type (defaults to the connected wallet's type).
connectorType?: string
// Override the wallet client type (defaults to the connected wallet's ID).
walletClientType?: string
// Callback when SIWE authentication fails.
onError?: (message: string, error?: OpenfortError) => void
// Callback when SIWE authentication succeeds.
onConnect?: () => void
// Link wallet to existing account instead of creating new one.
// Defaults to true if user is already authenticated.
link?: boolean
}Behavior
- If a user is already authenticated, the wallet will be linked to their existing account
- If no user is authenticated, a new account will be created using the wallet
- The hook requires a wallet to be connected via wagmi before calling
- Automatically handles chain switching if the account chain differs from the current chain
Related
- useWalletAuth - Higher-level hook that combines wallet connection and SIWE