/
Contact usSee pricingStart building

    About B2B Saas Authentication

    Introduction
    Stytch B2B Basics
    Integration Approaches
      Full-stack overview
      Frontend (pre-built UI)
      Frontend (headless)
      Backend
    Next.js
      Routing
      Authentication
      Sessions
    Migrations
      Overview
      Reconciling data models
      Migrating user data
      Additional migration considerations
      Zero-downtime deployment
      Defining external IDs for members
      Exporting from Stytch
    Custom Domains
      Overview

    Authentication

    Single Sign On
    • Resources

      • Overview
        External SSO Connections
        Standalone SSO
    • Integration Guides

      • Start here
        Backend integration guide
        Headless integration guide
        Pre-built UI integration guide
    OAuth
    • Resources

      • Overview
        Authentication flows
        Identity providers
        Google One Tap
        Provider setup
    • Integration Guides

      • Start here
        Backend integration
        Headless frontend integration
        Pre-built UI frontend integration
    Connected AppsBeta
      Setting up Connected Apps
      About Remote MCP Servers
    • Resources

      • Integrate with AI agents
        Integrate with a remote MCP server
    Sessions
    • Resources

      • Overview
        JWTs vs Session Tokens
        How to use Stytch JWTs
        Custom Claims
    • Integration Guides

      • Start here
        Backend integration
        Frontend integration
    Email OTP
      Overview
    Magic Links
    • Resources

      • Overview
        Email Security Scanner Protections
    • Integration Guides

      • Start here
        Backend integration
        Headless frontend integration
        Pre-built UI frontend integration
    Multi-Factor Authentication
    • Resources

      • Overview
    • Integration Guides

      • Start here
        Backend integration
        Headless frontend integration
        Pre-built UI frontend integration
    Passwords
      Overview
      Strength policies
    UI components
      Overview
      Implement the Discovery flow
      Implement the Organization flow
    DFP Protected Auth
      Overview
      Setting up DFP Protected Auth
      Handling challenges
    M2M Authentication
      Authenticate an M2M Client
      Rotate client secrets
      Import M2M Clients from Auth0

    Authorization & Provisioning

    RBAC
    • Resources

      • Overview
        Stytch Resources & Roles
        Role assignment
    • Integration Guides

      • Start here
        Backend integration
        Headless frontend integration
    SCIM
    • Resources

      • Overview
        Supported actions
    • Integration Guides

      • Using Okta
        Using Microsoft Entra
    Organizations
      Managing org settings
      JIT Provisioning

    Testing

    E2E testing
    Sandbox values
Get support on SlackVisit our developer forum

Contact us

B2B Saas Authentication

/

Guides

/

Authentication

/

OAuth

/

Integration Guides

/

Headless frontend integration

Headless Integration of OAuth

In Stytch’s B2B product there are two different versions of the OAuth authentication flow:

  1. Discovery Authentication: used for self-serve Organization creation or login prior to knowing the Organization context
  2. Organization-specific Authentication: used when you already know the Organization that the end user is trying to log into

This guide walks through how to offer OAuth for both scenarios.

Discovery Sign-Up or Login

The discovery flow is designed for situations where your end users are signing up or logging in from a central landing page, and have not specified which organization they are trying to access or are attempting to create a new Organization.

The sequence for how this flow works when using a headless integration approach is as follows:

Headless integration of discovery OAuth

1
Complete config steps

If you haven't done so already:

  1. Complete the steps in the OAuth Integration Guide Start Here
  2. Enable the Frontend SDKs in your Stytch Dashboard
  3. Once the FE SDK is enabled, enable Create Organizations under "Enabled Methods"

2
Configure discovery start

In your application’s UI, introduce oauth.discovery.$provider.start() to allow users to begin the OAuth discovery flow.

For example, to add Google OAuth you would do the following in our React SDK:

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>;
};

To add Google OAuth with our vanilla JS SDK you would add:

<script>
  import { StytchB2BHeadlessClient } from '@stytch/vanilla-js/b2b/headless';
  const stytch = new StytchB2BHeadlessClient('STYTCH_PUBLIC_TOKEN');

  document.getElementById('login-with-oauth').onclick = () => stytch.oauth.google.discovery.start();
</script>

<button id="login-with-oauth">Login with Google</button>

3
Configure callback

Stytch will send a callback to the Discovery RedirectURL you specified in the Stytch dashboard earlier. Upon receiving this callback you will exchange the token in the call for a list of Discovered Organizations that the user can choose to log into. An intermediate_session_token (IST) will also be returned, but the headless SDK will take care of setting this in cookies and including it in the follow up calls.

In React this looks like:

import React, { useEffect } from 'react';
import { useStytchB2BClient, useStytchMemberSession } from '@stytch/react/b2b';

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

  useEffect(() => {
    if (session) {
      window.location.href = 'https://example.com/profile';
    } else {
      const token = new URLSearchParams(window.location.search).get('token');
      stytchClient.oauth.discovery.authenticate({
        discovery_oauth_token: token,
      });
    }
  }, [stytchClient, session]);

  return <div>Loading</div>;
};

In vanilla Javascript this looks like:

<script>
  import { StytchB2BHeadlessClient } from '@stytch/vanilla-js/b2b/headless';
  const stytch = new StytchB2BHeadlessClient('STYTCH_PUBLIC_TOKEN');

  const token = new URLSearchParams(window.location.search).get('token');
  stytch.oauth.discovery.authenticate({
    discovery_oauth_token: token
  });
</script>

4
Handle user selection

If the end user selects to login to an existing Organization, you can call the exchange intermediate session method with the selected OrgID.

For example, in React you would add:

import React, { 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>;
};

In vanilla Javascript you would add the following:

// in JavaScript
import { StytchB2BHeadlessClient } from '@stytch/vanilla-js/b2b/headless';
  const stytch = new StytchB2BHeadlessClient('STYTCH_PUBLIC_TOKEN');
  const exchangeIntermediateSession = () => {
    stytch.discovery.intermediateSessions.exchange({
      organization_id: 'organization-test-07971b06-ac8b-4cdb-9c15-63b17e653931'
    });
  };

  // in HTML
  <button onclick="exchangeIntermediateSession()">Log In</button>

You can also optionally allow users the ability to create a new Organization instead of logging into an existing one. In order to enable this, ensure that you have enabled Create Organizations in the FE SDKs section of the Stytch Dashboard.

To support creating a new Organization through discovery in your application, add the following code with our React SDK:

import React, { 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>;
};

Or with our vanilla Javascript SDK:

// in JavaScript
import { StytchB2BHeadlessClient } from '@stytch/vanilla-js/b2b/headless';
const stytch = new StytchB2BHeadlessClient('public-token-test-678e2e02-0db0-45c6-a52f-29b3f3b90673');
const createOrganization = () => {
  stytch.discovery.organizations.create();
};

// in HTML
<button onclick="createOrganization()">Create Organization</button>

You can also optionally prompt the user for the name and slug of their new organization, but if not provided Stytch will auto generate a name and slug based on the end user’s email address.

5
Test it out

Run your application and test out doing discovery login and sign-up!

Organization Login

If end users of your application login via a page that indicates which Organization they are trying to log into (e.g. <org-slug>.your-app.com or your-app.com/team/<org-slug>) you can offer organization login on that page.

The high level flow using our headless SDK is as follows:

Headless integration of organization-specific OAuth

1
Complete config steps

If you haven't done so already:

  1. Complete the steps in the OAuth Integration Guide Start Here
  2. Enable the Frontend SDKs in your Stytch Dashboard

2
Configure login start

In your application’s UI use the oauth.$provider.start() method to initiate the OAuth flow. If you are building google this would look as follows using our React SDK:

import { useStytchB2BClient } from '@stytch/react/b2b';

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

  const startOAuth = () =>
    stytchClient.oauth.google.start({
      slug: 'your-org-slug'
    });

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

In vanilla Javascript this would look as follows:

<script>
  import { StytchB2BHeadlessClient } from '@stytch/vanilla-js/b2b/headless';
  const stytch = new StytchB2BHeadlessClient('STYTCH_PUBLIC_TOKEN');

  document.getElementById('login-with-oauth').onclick = () => stytch.oauth.google.start({
    slug: 'your-org-slug'
  });
</script>

<button id="login-with-oauth">Login with Google</button>

3
Configure callback

Stytch will redirect the user to the Login or Signup RedirectURL that you specified in the oauth.$provider.start() call, or the default in the Stytch dashboard. The URL’s query parameters will contain stytch_token_type=oauth and an authentication token. Your application should extract the token from the URL and call the appropriate authentication method to finish the login process.

In our React SDK you would add the following:

import React, { useEffect } from 'react';
import { useStytchB2BClient, useStytchMemberSession } from '@stytch/react/b2b';

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

  useEffect(() => {
    if (session) {
      window.location.href = 'https://example.com/profile';
    } else {
      const token = new URLSearchParams(window.location.search).get('token');
      stytchClient.oauth.authenticate({
        oauth_token: token
      });
    }
  }, [stytchClient, session]);

  return <div>Loading</div>;
};

In vanilla Javascript you would add:

<script>
  import { StytchB2BHeadlessClient } from '@stytch/vanilla-js/b2b/headless';
  const stytch = new StytchB2BHeadlessClient('STYTCH_PUBLIC_TOKEN');

  const token = new URLSearchParams(window.location.search).get('token');
  stytch.oauth.authenticate({
    oauth_token: token
  });
</script>

4
Test it out

Run your application and test out the flow using the Organization that you created earlier through discovery!

Discovery Sign-Up or Login

1.

Complete config steps

2.

Configure discovery start

3.

Configure callback

4.

Handle user selection

5.

Test it out

Organization Login

1.

Complete config steps

2.

Configure login start

3.

Configure callback

4.

Test it out