Authentication

This guide covers the complete wallet authentication flow using EVM wallets.

Step-by-Step Authentication

Step 1: Generate Nonce

First, request a nonce (unique message) for the user's address:

import { YieldFiSDK } from "yieldfi-sdk";

const sdk = await YieldFiSDK.create({
  gatewayUrl: "https://gw.yield.fi",
});

// Generate nonce for user's address
const nonce = await sdk.auth.generateNonce({
  address: "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
});

console.log(nonce.message);
// "Sign this message to authenticate with YieldFi.\n\nAddress: 0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb\nNonce: abc123..."

Step 2: Sign Message with Wallet

Sign the message using the user's wallet. Here are examples for different wallet providers:

Using ethers.js v6

Using MetaMask Directly

Using WalletConnect

Step 3: Login with Signature

Submit the signature to complete authentication:

Step 4: Store Tokens

Store the tokens securely in your application:

Complete Example

Here's a complete React component example:

Error Handling

Handle common authentication errors:

Security Best Practices

  1. Always verify the message - Users should verify the message they're signing matches what they expect

  2. Use HTTPS - Always use HTTPS in production to protect tokens

  3. Secure storage - Consider using secure storage mechanisms (HTTP-only cookies for server-side apps)

  4. Token expiration - Implement token refresh logic (see Token Management)

  5. Logout on errors - Clear tokens if authentication fails

Next Steps

  • Token Management - Learn how to refresh and manage tokens

  • User Consent - Manage user consent records

Last updated