npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@supertokens-plugins/tenant-discovery-react

v0.1.0

Published

Tenant Discovery Plugin for SuperTokens

Readme

SuperTokens Plugin Tenant Discovery

Add tenant discovery functionality to your SuperTokens React application. This plugin provides a user-friendly interface for users to select tenants and integrates seamlessly with the backend tenant discovery plugin.

Installation

npm install @supertokens-plugins/tenant-discovery-react

Quick Start

Frontend Configuration

Initialize the plugin in your SuperTokens frontend configuration:

import SuperTokens from "supertokens-auth-react";
import TenantDiscoveryPlugin from "@supertokens-plugins/tenant-discovery-react";

SuperTokens.init({
  appInfo: {
    // your app info
  },
  recipeList: [
    // your recipes
  ],
  experimental: {
    plugins: [
      TenantDiscoveryPlugin.init({
        showTenantSelector: true, // Optional: defaults to true
        extractTenantIdFromDomain: true, // Optional: defaults to true
      }),
    ],
  }
});

[!IMPORTANT]
You also have to install and configure the backend plugin for full tenant discovery functionality.

Tenant Selection Interface

The plugin provides a complete tenant selection interface accessible at /tenant-discovery/select. This page includes:

  • Tenant List: Displays all available tenants from your SuperTokens configuration
  • Tenant Selection: Dropdown to choose from available tenants
  • Automatic Redirect: Redirects to authentication with selected tenant ID
  • Loading States: Proper loading and error handling

Configuration Options

| Option | Type | Default | Description | | --------------------------- | ------- | ------- | ---------------------------------------------- | | showTenantSelector | boolean | true | Whether to show the tenant selector interface | | extractTenantIdFromDomain | boolean | true | Whether to extract tenant ID from email domain |

Hooks and Utilities

Besides the default UI, the plugin exposes some hooks and util functions that can be used directly (e.g. building a custom UI).

usePluginContext Hook

Access exposed plugin functionality in your custom components:

import { usePluginContext } from "@supertokens-plugins/tenant-discovery-react";

function MyTenantComponent() {
  const { api, functions } = usePluginContext();

  const handleFetchTenants = async () => {
    try {
      const response = await api.fetchTenants();
      if (response.status === "OK") {
        console.log("Available tenants:", response.tenants);
      }
    } catch (error) {
      console.error("Failed to fetch tenants:", error);
    }
  };

  const handleSetTenant = (tenantId: string, email?: string) => {
    functions.setTenantId(tenantId, email, true);
  };

  return (
    <div>
      {/* Your custom tenant selection UI */}
    </div>
  );
}

API Methods

The plugin provides these API methods through the usePluginContext hook:

fetchTenants

Retrieve all available tenants:

const { api } = usePluginContext();

// Fetch all tenants
const result = await api.fetchTenants();
if (result.status === "OK") {
  console.log("Tenants:", result.tenants);
  // result.tenants is an array of { tenantId: string }
} else {
  console.error("Error:", result.message);
}

Plugin Methods

Access tenant management functionality:

setTenantId

Set the current tenant ID:

const { functions } = usePluginContext();

// Save email for prefilling the email input after reload
functions.setEmailId("[email protected]");
// Set tenant ID and refresh
functions.setTenantId("company-tenant", true);

determineTenantFromURL

Extract tenant from URL parameters:

const { functions } = usePluginContext();

// Get tenant from URL
const tenantFromURL = await functions.determineTenantFromURL();
console.log("URL Tenant:", tenantFromURL);

determineTenantFromSubdomain

Extract tenant from subdomain:

const { functions } = usePluginContext();

// Get tenant from subdomain
const tenantFromSubdomain = await functions.determineTenantFromSubdomain();
console.log("Subdomain Tenant:", tenantFromSubdomain);

Usage Patterns

[!NOTE]
All the following functionalities are already part of the plugin. Following examples are shown for usage with custom UI's etc.

Email-Based Tenant Discovery

Use the plugin with backend tenant discovery for seamless user routing:

import { usePluginContext } from "@supertokens-plugins/tenant-discovery-react";

function EmailTenantDiscovery() {
  const { functions, api } = usePluginContext();
  const [email, setEmail] = useState("");

  const handleEmailSubmit = async () => {
    const result = await api.tenantIdFromEmail(email);
    if (result.status === "OK") {
      // Set discovered tenant
      functions.setTenantId(result.tenant, email, true);
    }
  };

  return (
    <div>
      <input
        type="email"
        value={email}
        onChange={(e) => setEmail(e.target.value)}
        placeholder="Enter your email"
      />
      <button onClick={handleEmailSubmit}>Continue</button>
    </div>
  );
}

Custom Tenant Selector

Create your own tenant selection interface:

import { usePluginContext } from "@supertokens-plugins/tenant-discovery-react";

function CustomTenantSelector() {
  const { api, functions } = usePluginContext();
  const [tenants, setTenants] = useState([]);

  useEffect(() => {
    const loadTenants = async () => {
      const response = await api.fetchTenants();
      if (response.status === "OK") {
        setTenants(response.tenants);
      }
    };
    loadTenants();
  }, [api]);

  const handleTenantSelect = (tenantId: string) => {
    functions.setTenantId(tenantId, undefined, true);
    // Redirect to auth
    redirectToAuth({ queryParams: { tenantId } });
  };

  return (
    <div>
      <h2>Choose Your Organization</h2>
      {tenants.map((tenant) => (
        <button
          key={tenant.tenantId}
          onClick={() => handleTenantSelect(tenant.tenantId)}
        >
          {tenant.tenantId === 'public' ? 'Personal Account' : tenant.tenantId}
        </button>
      ))}
    </div>
  );
}

Requirements

  • SuperTokens React SDK >= 0.50.0
  • SuperTokens Tenant Discovery Backend Plugin (recommended)
  • Multi-tenancy must be properly configured

Related