Web3Auth Web

repository·master·Indexed 19 days ago

https://github.com/web3auth/web3auth-web

A monorepo for Web3Auth web packages providing a non-custodial authentication experience. It aggregates OAuth logins (Google, Twitter, Discord) and uses Multi-Party Computation (MPC) for key management. The repository includes the @web3auth/modal SDK with dedicated support for React and Vue integrations, as well as various demo applications for Next.js, EVM-compatible chains, and the x402 payment protocol.

Tokens
37.8K
Snippets
99
Records
209
Agent score
67%

What's inside web3auth-web

  1. Understand Authentication Modes: `connect-and-sign` vs `connect-only`

    master

    The SDK supports two primary authentication modes during the connection process:

    1. connect-and-sign: connectTo() resolves only after both the connection and the authorization (signing) are complete. External wallets must complete the auth challenge and return a usable ID token.
    2. connect-only: connectTo() resolves immediately after the connection step is successful, without triggering an unnecessary authentication challenge.

    Ensure that switching between these modes across app reloads does not leave stale state and that failed authorization steps leave the connector in a recoverable state.

  2. Standardized Error Handling and Behavior for Web3Auth Hooks

    master

    When working with Web3Auth hooks (specifically in React and Vue), ensure you are using the standardized error types and state behaviors to maintain consistency across @web3auth/modal and @web3auth/no-modal.

    Key behavioral standards include:

    • Error Types: Use WalletInitializationError.notReady() for missing-wallet errors in useSignMessage.
    • Vue Error Handling: Use Web3Auth-style errors instead of plain Error objects in useWalletServicesPlugin.
    • Connection Status: When checking connection status in WalletServicesContextProvider, use CONNECTED_STATUSES.includes() rather than checking for a single CONNECTED string.
    • State Derivation: For Solana wallet accounts, prefer memoized derivation over useState + useEffect patterns.
    • Loading/Error States: Ensure consistent ordering of loading and error states in useWalletConnectScanner.
  3. Identify the canonical implementation for shared features

    master

    When deciding where logic or integrations live, use this ownership model:

    AreaCanonical implementationWhat stays local in modal
    React core hooks@web3auth/no-modal/reactuseWeb3AuthInner, useWeb3Auth, useWeb3AuthConnect, provider/context wiring
    Vue core composables@web3auth/no-modal/vueuseWeb3AuthInner, useWeb3Auth, useWeb3AuthConnect, provider wiring
    React Solana@web3auth/no-modal/react/solanathin public re-export files only
    Vue Solana@web3auth/no-modal/vue/solanathin public re-export files only
    x402@web3auth/no-modal/x402/reactthin public re-export files only
    WagmiPending follow-upPending follow-up
  4. Identify shared vs. local hooks and providers

    master

    When integrating Web3Auth, it is important to understand which logic is shared across the ecosystem and which is specific to the Modal implementation.

    Shared Logic (from @web3auth/no-modal)

    Most core React hooks and Vue composables are shared implementations designed to work with the canonical shared context.

    Local Logic (specific to @web3auth/modal)

    The following are kept local to the Modal package to handle specific lifecycle and UI requirements:

    CategoryKeep local in modal
    React CoreuseWeb3Auth, useWeb3AuthConnect, useWeb3AuthInner, providers, context provider implementations
    React Solanaprovider.ts
    Vue CoreuseWeb3Auth, useWeb3AuthConnect, useWeb3AuthInner, providers
    Vue Solanaprovider.ts, useSolanaClient.ts
    WagmiProviders (in the first pass)
  5. Verify Rehydration and Persisted State

    master

    The SDK supports rehydrating sessions from cached state. Validate the following:

    Session Rehydration

    • Auth: Rehydrates if the session is valid; triggers error path if expired.
    • MetaMask/Injected: Rehydrates if wallet permission/access is still granted.
    • WalletConnect: Rehydrates if the WC session still exists.
    • Linked Accounts: If the active account is a linked external wallet, a reload should restore that linked account as active with the correct provider and chain.

    Storage Backends

    • Ensure state restoration works via localStorage, cookie-backed state (if used), and the initialState provided in the constructor.
  6. Understand the React Context deduplication strategy

    master

    Web3Auth uses a shared React context strategy to ensure that both @web3auth/modal and @web3auth/no-modal packages operate on the same underlying identity.

    Key Concept: Context Identity First To prevent bugs where hooks from one package cannot access the provider from another, the @web3auth/no-modal package acts as the canonical owner of the React context objects. The @web3auth/modal package is designed to publish its state into these shared no-modal context identities.

    Implementation Pattern: Thin Re-exports To maintain backward compatibility and avoid breaking existing imports in the modal package, the modal package uses a 'thin re-export' pattern. Instead of defining its own context via createContext(), it imports the shared context from @web3auth/no-modal/react and re-exports it from its own local paths. This allows existing code to continue using import { ... } from '@web3auth/modal/react/context/...' while actually consuming the shared identity.

  7. Understand the relationship between @web3auth/modal and @web3auth/no-modal

    master

    Web3Auth uses two primary packages for integration: @web3auth/modal (Plug and Play Modal SDK) and @web3auth/no-modal (for custom UI implementations).

    To ensure consistency, @web3auth/no-modal serves as the canonical source of truth for shared logic. Most core implementations for React hooks, Vue composables, Solana integrations, and x402 logic reside in no-modal.

    @web3auth/modal acts as an API-compatible layer that:

    1. Re-exports shared logic from no-modal via public entrypoints.
    2. Provides thin adapters for modal-specific behaviors (e.g., specific Web3Auth class typing, MFA fields in Vue, or the zero-argument connect() flow).
    3. Manages modal-specific provider wiring.
  8. How the x402 payment flow works

    master

    The x402 payment flow integrates middleware on the server with a specialized hook on the client:

    1. Interception: The paymentMiddleware from @x402/express intercepts requests to protected routes.
    2. Challenge: If the request lacks a valid X-PAYMENT header, the server responds with HTTP 402 and a JSON body containing payment instructions.
    3. Client Payment: A client-side hook (like useX402Fetch) detects the 402, signs a micro-payment with the user's connected wallet, and retries the request with the X-PAYMENT header attached.
    4. Verification: The x402 facilitator verifies the on-chain payment, allowing the server to deliver the protected resource.
  9. Recommended Smoke Test Suite for @web3auth/modal

    master

    Perform this minimum smoke test pass to ensure core functionality is stable:

    1. Auth Flows: Social login and Email passwordless.
    2. Wallet Connections: MetaMask connect and WalletConnect v2 (test both successful QR connection and cancellation).
    3. Session Management:
      • Rehydration after connecting with Auth and one external wallet.
      • Logout after connecting with Auth and one external wallet.
    4. Chain/Namespace Management: Multichain switching within a namespace and cross-namespace switching.
    5. Account Lifecycle: Perform a full sequence of Account Link $\rightarrow$ Switch $\rightarrow$ Unlink on an Auth-connected session.
  10. Validate Multichain and Rehydration Behavior

    master

    Ensure the SDK handles multiple chains and session persistence correctly.

    Multichain Logic

    • Namespace Selection: Multichain-compatible wallets should show a namespace picker (e.g., choosing EVM vs. Solana).
    • Runtime switchChain: Switching chains within a namespace must update the active provider and downstream hooks.
    • Cross-namespace Switching: Verify Auth and WalletConnect v2 support switching between EVM and Solana when configured. Single-namespace connectors should return a clear error if a user attempts to cross namespaces.

    Session Rehydration

    • Primary Session Restore: Reloading the page after any successful login (Auth, MetaMask, WalletConnect, or Injected) must restore the expected primary session.
    • Edge Cases:
      • Expired Auth sessions should clear the cached connector and fall back to READY.
      • Missing WalletConnect sessions should clear the stale cache.
      • Rehydration must restore the correct chain for multichain connectors and restore active linked accounts.
  11. Import shared code from @web3auth/no-modal

    master

    When working within the monorepo or consuming these packages, never use relative cross-package source imports (e.g., ../../../no-modal/src/...). This breaks package boundaries and distribution builds.

    Always import shared code through the supported public entrypoints:

    • @web3auth/no-modal/react
    • @web3auth/no-modal/vue
    • @web3auth/no-modal/react/solana
    • @web3auth/no-modal/vue/solana
    • @web3auth/no-modal/x402/react