useAuthorization
A low-level hook for managing wallet authorization.
The useAuthorization hook is a low-level hook for managing wallet authorization. It is used internally by the useMobileWallet hook and is not meant to be used directly in most cases.
Parameters
The hook accepts an object with the following properties:
| 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 that uses AsyncStorage will be used.
The Cache Object
The Cache object is an interface that defines the contract for a cache implementation. It has 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. |
You can provide your own implementation of the Cache interface to customize how authorization data is stored.
Return Value
The hook returns an object with the following properties:
| 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. |
The Account Object
The Account object represents a user's wallet account that has been authorized for use with the dApp. It has the following properties:
| 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
import { useAuthorization } from '@wallet-ui/react-native-web3js';
import { useCallback } from 'react';
import { Button, Text, View } from 'react-native';
function MyComponent() {
const { accounts, authorizeSession, selectedAccount } = useAuthorization({
chain: 'solana:mainnet',
identity: {
name: 'My App',
},
});
const handleConnect = useCallback(async () => {
// This is a simplified example. In a real app, you would
// get the wallet object from a wallet adapter.
const wallet = getWallet();
await authorizeSession(wallet);
}, [authorizeSession]);
return (
<View>
{selectedAccount ? (
<Text>Connected to {selectedAccount.address}</Text>
) : (
<Button title="Connect" onPress={handleConnect} />
)}
</View>
);
}