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.
Cache contract
Section titled “Cache contract”Both variants accept a cache implementation with the following methods:
| Name | Type | Description |
|---|---|---|
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.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
cache | WalletAuthorizationCache | Optional. A cache instance for storing authorization data. |
chain | Chain | The blockchain chain to connect to. |
identity | AppIdentity | The identity of the app connecting to the wallet. |
store | AuthorizationStore | The authorization store instance. |
These parameters are provided automatically when useAuthorization is used inside MobileWalletProvider.
Return value
Section titled “Return value”| Name | Type | Description |
|---|---|---|
accounts | Account[] | null | An 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. |
selectedAccount | Account | null | The currently selected wallet account. |
Account object
Section titled “Account object”| Name | Type | Description |
|---|---|---|
address | Address | The wallet address as an Address type from @solana/kit. |
addressBase64 | Base64EncodedAddress | The base64-encoded address. |
icon | WalletIcon | Optional. The wallet icon. |
label | string | undefined | An optional, user-defined label for the account. |
Example
Section titled “Example”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> );}Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
cache | Cache<WalletAuthorization> | undefined | Optional. A cache instance for storing authorization data. |
chain | Chain | The blockchain chain to connect to. |
identity | AppIdentity | The identity of the app connecting to the wallet. |
If no cache is provided, a default cache backed by AsyncStorage is used.
Return value
Section titled “Return value”| Name | Type | Description |
|---|---|---|
accounts | Account[] | null | An 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. |
isLoading | boolean | A boolean indicating if the authorization state is loading. |
selectedAccount | Account | null | The currently selected wallet account. |
Account object
Section titled “Account object”| Name | Type | Description |
|---|---|---|
address | string | The base58-encoded string representation of the public key. |
label | string | undefined | An optional, user-defined label for the account. |
publicKey | PublicKey | A PublicKey object from @solana/web3.js. |
Example
Section titled “Example”import { useAuthorization } from '@wallet-ui/react-native-web3js';import { useCallback } from 'react';import { Button, Text, View } from 'react-native';
function MyComponent() { const { authorizeSession, selectedAccount } = useAuthorization({ chain: 'solana:mainnet', identity: { name: 'My App' }, });
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> );}