Skip to content

useAuthorization

The useAuthorization hook is a low-level hook for managing wallet authorization. It is used internally by useMobileWallet and is mainly useful when you need direct control over authorization state.

Both variants accept a cache implementation with the following methods:

NameTypeDescription
clear() => Promise<void>Clears the cache.
get() => Promise<T | undefined>Retrieves an item from the cache.
set(value: T) => Promise<void>Sets an item in the cache.

The authorization flow is shared across both packages. The main differences are the hook parameters, the returned loading/store-related fields, and the account types.

NameTypeDescription
cacheWalletAuthorizationCacheOptional. A cache instance for storing authorization data.
chainChainThe blockchain chain to connect to.
identityAppIdentityThe identity of the app connecting to the wallet.
storeAuthorizationStoreThe authorization store instance.

These parameters are provided automatically when useAuthorization is used inside MobileWalletProvider.

NameTypeDescription
accountsAccount[] | nullAn array of all authorized accounts.
authorizeSession(wallet: AuthorizeAPI) => Promise<Account>A function to authorize a new session.
authorizeSessionWithSignIn(wallet: AuthorizeAPI, signInPayload: SignInPayload) => Promise<Account>A function to authorize a new session with sign-in.
deauthorizeSession(wallet: DeauthorizeAPI) => Promise<void>A function to deauthorize the current session.
deauthorizeSessions() => Promise<void>A function to deauthorize all sessions.
selectedAccountAccount | nullThe currently selected wallet account.
NameTypeDescription
addressAddressThe wallet address as an Address type from @solana/kit.
addressBase64Base64EncodedAddressThe base64-encoded address.
iconWalletIconOptional. The wallet icon.
labelstring | undefinedAn optional, user-defined label for the account.
import { createAsyncStorageCache, createAuthorizationStore, useAuthorization } from '@wallet-ui/react-native-kit';
import { useCallback } from 'react';
import { Button, Text, View } from 'react-native';
const cache = createAsyncStorageCache();
const store = createAuthorizationStore({ cache });
function MyComponent() {
const { authorizeSession, selectedAccount } = useAuthorization({
chain: 'solana:mainnet',
identity: { name: 'My App' },
store,
cache,
});
const handleConnect = useCallback(async () => {
const wallet = getWallet();
await authorizeSession(wallet);
}, [authorizeSession]);
return (
<View>
{selectedAccount ? (
<Text>Connected to {selectedAccount.address}</Text>
) : (
<Button title="Connect" onPress={handleConnect} />
)}
</View>
);
}