Overview
Staxiq uses @stacks/connect for wallet authentication, providing seamless integration with popular Stacks wallets like Leather, Xverse, and Asigna. Authentication persists across sessions using the UserSession API.Authentication Flow
The authentication system is managed through theuseWallet hook, which handles:
- Wallet connection prompts
- Session persistence
- Network detection (testnet/mainnet)
- Address extraction
- Disconnection and cleanup
1
Initialize App Config
Configure app permissions and create a UserSession instance.
2
Trigger Authentication
Call the
authenticate function with app details and callbacks.3
Check Existing Session
On app load, check if user is already authenticated.
4
Handle Disconnection
Sign out and clear session data.
Using the useWallet Hook
The recommended way to handle authentication is through theuseWallet hook:
Network Detection
Staxiq automatically detects the correct network based on the user’s address:boolean
Addresses starting with
ST use testnetboolean
Addresses starting with
SP use mainnetSession Persistence
User sessions are automatically persisted in browser storage. When users return to your app:App Configuration
Required Permissions
string
required
Allows the app to write to Gaia storage (user’s decentralized storage)
string
required
Enables publishing data to the blockchain
App Details
string
required
Application name shown in wallet connection prompt
string
required
App icon URL displayed during authentication (must be absolute URL)
User Data Structure
After successful authentication, the user data object contains:object
Error Handling
Handle authentication errors gracefully:Logout Flow
Properly clean up user sessions on logout:The page reload ensures all components and contexts are reset to their unauthenticated state.
Address Formatting
Utility function to display shortened addresses:Complete Example
Best Practices
Loading States
Always show loading indicators during wallet connection to improve UX
Error Recovery
Provide clear error messages and retry options for failed connections
Session Checking
Check for existing sessions on app load to auto-reconnect users
Clean Logout
Always call signUserOut() and reload to prevent state inconsistencies