Xumm OAuth2 PKCE Client SDK

2.8.7 · active · verified Wed Apr 22

The `xumm-oauth2-pkce` JavaScript SDK facilitates client-side only OAuth2 Authorization Code with PKCE (Proof Key for Code Exchange) flow for the Xumm ecosystem. Currently at version 2.8.7, this package provides a secure method for web applications to authenticate users with Xumm without requiring a backend server for token exchange. It is actively maintained and typically follows a release cadence tied to Xumm ecosystem updates. Key differentiators include its focus on client-side security via PKCE, out-of-the-box handling of browser redirects and session persistence (using `localStorage` by default), and offering both event-driven and promise-based APIs. It integrates seamlessly into browser environments, abstracting away the complexities of the OAuth2 PKCE flow for Xumm users, and ships with TypeScript types for enhanced development experience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `XummPkce`, handle authentication events (success, retrieval, errors), and manage user login/logout state within a simple browser application. It uses both event listeners and `authorize()` for interactive authentication.

import { XummPkce } from 'xumm-oauth2-pkce';

// Replace with your actual Xumm API key or load from environment
const XUMM_API_KEY = process.env.XUMM_API_KEY ?? 'your-xumm-api-key-uuidv4';

const xumm = new XummPkce(XUMM_API_KEY);

const handleAuthResult = async () => {
  const state = await xumm.state();
  if (state && state.me) {
    const { sdk, me } = state;
    console.log("Successfully authenticated with Xumm!");
    console.log("User account:", me.account);
    console.log("Xumm SDK instance:", sdk); // This is an instance of xumm-sdk
    document.getElementById('auth-status')!.innerText = `Authenticated as ${me.account}`;
    document.getElementById('auth-button')!.innerText = 'Logout';
  } else {
    console.log("Not authenticated.");
    document.getElementById('auth-status')!.innerText = 'Not authenticated.';
    document.getElementById('auth-button')!.innerText = 'Sign in with Xumm';
  }
};

// Listen for retrieved session (e.g., after page refresh or mobile redirect)
xumm.on("retrieved", handleAuthResult);
// Listen for new successful authentication
xumm.on("success", handleAuthResult);
// Listen for errors
xumm.on("error", (error) => {
  console.error("Xumm authentication error:", error);
  document.getElementById('auth-status')!.innerText = `Authentication failed: ${error.message}`;
  xumm.logout(); // Clear any partial state
});

// Check authentication status on page load and set up interaction
document.addEventListener('DOMContentLoaded', async () => {
  document.body.innerHTML = `
    <div id="app" style="font-family: sans-serif; padding: 20px;">
      <h1>Xumm PKCE Auth Demo</h1>
      <p id="auth-status">Checking authentication status...</p>
      <button id="auth-button">Loading...</button>
    </div>
  `;

  document.getElementById("auth-button")!.onclick = async () => {
    const currentState = await xumm.state();
    if (currentState && currentState.me) {
      await xumm.logout();
      console.log("Logged out.");
    } else {
      console.log("Initiating Xumm authorization...");
      // For a new sign-in, the authorize() call will redirect or open a popup.
      // The 'success' event will handle the result.
      await xumm.authorize();
    }
    handleAuthResult(); // Update button and status immediately after action
  };

  handleAuthResult(); // Initial check
});

view raw JSON →