Skip to main content

Overview

This guide walks through integrating email magic links using Stytch’s headless frontend SDKs. This approach gives you complete control over your UI while handling authentication logic on the frontend.Both and Organization-specific authentication flows are supported.
The Discovery flow is designed for situations where end users are signing up or logging in from a central landing page, and have not specified which they are trying to access.

Prerequisites

  1. Enable the Frontend SDKs in your Stytch Dashboard
  2. Enable Create Organizations under “Enabled Methods”

Implementation

1

Initiate magic link email

In your application’s UI, introduce a way for end users to input their email and trigger a magicLinks.email.discovery.send() method call with the provided email.
import { useStytchB2BClient } from '@stytch/react/b2b';

export const Login = () => {
  const stytch = useStytchB2BClient();

  const sendDiscoveryEmail = () => {
      stytch.magicLinks.email.discovery.send({
          email_address: 'sandbox@stytch.com',
      });
  };

  return <button onClick={sendDiscoveryEmail}>Send email</button>;
};
2

Handle callback

Stytch will redirect the user to the Discovery RedirectURL you specified on the Redirect URLs page earlier. Exchange the token for a list of Discovered Organizations that the user can choose to log into.
export const DiscoveryAuthenticate = () => {
  const stytch = useStytchB2BClient();

  useEffect(() => {
    const authenticate = async () => {
      const result = await stytch.authenticateByUrl();

      if (result?.handled) {
        const { email_address, discovered_organizations } = result.data;
        // surface discovered organizations to user to select from
        console.log({ email_address, discovered_organizations });
      }
    };

    authenticate();
  }, [stytch]);

  return <div>Loading</div>;
};
An will be returned. The headless SDK will automatically set this in cookies and include it in follow-up calls.
3

Handle organization selection

When the end user selects an Organization to log into, call the exchange intermediate session method with the selected Organization ID.
import { useEffect } from 'react';
import { useStytchB2BClient } from '@stytch/react/b2b';

export const ExchangeIntermediateSession = () => {
  const stytch = useStytchB2BClient();

  useEffect(() => {
    stytch.discovery.intermediateSessions.exchange({
      organization_id: 'organization-test-07971b06-ac8b-4cdb-9c15-63b17e653931'
    });
  });

  return <div>Log In</div>;
};
4

(Optional) Support organization creation

Allow users to create a new Organization instead of logging into an existing one.
import { useEffect } from 'react';
import { useStytchB2BClient } from '@stytch/react/b2b';

export const CreateOrganization = () => {
  const stytch = useStytchB2BClient();

  useEffect(() => {
    stytch.discovery.organizations.create();
  });

  return <div>Create Organization</div>;
};
You can optionally prompt the user for the name and slug of their new Organization. If not provided, Stytch will auto-generate them based on the end user’s email address.