Skip to main content
Before you begin, make sure you’ve completed the OAuth configuration steps.

Overview

This guide walks through integrating OAuth 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. Complete the steps in the OAuth configuration guide
  2. Enable the Frontend SDKs in your Stytch Dashboard
  3. Enable Create Organizations under “Enabled Methods”

Implementation

1

Start the OAuth Discovery flow

In your application’s UI, use oauth.discovery.$provider.start() to allow users to begin the OAuth Discovery flow.
import { useStytchB2BClient } from '@stytch/react/b2b';

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

  const startOAuth = () =>
    stytchClient.oauth.google.discovery.start();

  return <button onClick={startOAuth}>Log in with Google</button>;
};
2

Handle the OAuth callback

Stytch will send a callback to the Discovery Redirect URL you specified in the Stytch Dashboard. Exchange the token for a list of Discovered Organizations that the user can choose to log into.
import { useEffect } from 'react';
import { useStytchB2BClient, useStytchMemberSession } from '@stytch/react/b2b';
import { useNavigate } from 'react-router';

export const Authenticate = () => {
  const stytchClient = useStytchB2BClient();
  const { session } = useStytchMemberSession();
  const navigate = useNavigate();

  useEffect(() => {
    if (session) {
      navigate('/profile');
    } else {
      stytchClient.authenticateByUrl({
        session_duration_minutes: 60,
      });
    }
  }, [stytchClient, session, navigate]);

  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.