> ## Documentation Index
> Fetch the complete documentation index at: https://stytch.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Adding to Stytch Login

> Integrate SSO with Stytch's pre-built UI components.

## Authentication flow

```mermaid theme={null}
sequenceDiagram
    participant Frontend as Stytch Login Component
    participant App as Your Application Logic
    participant Stytch as Stytch
    participant IDP as Workflow Idp

    Frontend->>Stytch: User initiates SSO login
    Stytch-->>Frontend: Returns redirect URL
    Frontend->>IDP: Automatically redirects to IdP
    IDP<<-->>Frontend: User authenticates with IdP
    IDP<<-->>Stytch: Stytch performs SSO handshake with IdP
    Stytch-->>Frontend: Redirects to Login or Signup Redirect URL with token
    Frontend-->>Stytch: SDK automatically authenticates token
    Stytch->>Frontend: Member logged in<br/>Session cookie stored in browser
```

## Prerequisites

If you haven't done so already:

1. If you'd like to have a provider to test with, complete the steps in the [SSO Provider Setup](https://stytch.com/docs/multi-tenant-auth/authentication/sso/provider-setup.md) guide for an Organization that you're a Member of.
2. Enable the Frontend SDKs in [your Stytch Dashboard](https://stytch.com/dashboard/sdk-configuration).

## Add SSO to your UI config

To surface SSO in the UI, include `B2BProducts.sso` in the `products` array of your configuration.

```jsx React icon="react" theme={null}
import { B2BProducts, StytchB2B } from '@stytch/react/b2b';

const Login = () => {
  const config = {
    authFlowType: "Discovery", // or "Organization"
    products: [B2BProducts.sso],
    ssoOptions: {},
  };

  return <StytchB2B config={config} />;
};

export default Login;
```

```jsx Next.js icon="triangle" theme={null}
import { B2BProducts, StytchB2B } from '@stytch/nextjs/b2b';

const Login = () => {
  const config = {
    authFlowType: "Discovery", // or "Organization"
    products: [B2BProducts.sso],
    ssoOptions: {},
  };

  return <StytchB2B config={config} />;
};

export default Login;
```

```html Vanilla JS icon="square-js" theme={null}
<!DOCTYPE html>
<html lang="en">
  <body>
    <div class="app">
      <div>
        <stytch-ui id="stytch-ui"></stytch-ui>
      </div>
    </div>
    <script>
      import { B2BProducts, createStytchB2BClient, StytchB2B } from '@stytch/vanilla-js/b2b';
      const stytch = createStytchB2BClient('PUBLIC_TOKEN');

      const config = {
        authFlowType: "Discovery", // or "Organization"
        products: [B2BProducts.sso],
        ssoOptions: {},
      };

      customElements.define('stytch-ui', StytchB2B);
      document.getElementById('stytch-ui').render({
        client: stytch,
        config,
      });
    </script>
  </body>
</html>
```

> [!NOTE]
>
> SSO can be used in both Discovery and Organization-specific flows. Use Discovery if you need to determine the user's Organization before completing authentication.

## Render the UI component

Render the Stytch UI component on the Redirect URL(s) that you specified in the [Stytch Dashboard](https://stytch.com/dashboard/redirect-urls) during setup.

When rendered, the UI component will automatically:

* Handle the SSO redirect based on the `stytch_token_type`
* For Discovery flows, prompt the user to select an Organization and finish authentication
* For Organization-specific flows, complete authentication directly

## Test your integration

First, ensure the Organization you will log into has an [sso provider configured](https://stytch.com/docs/multi-tenant-auth/authentication/sso/provider-setup.md). Then, start your application and test the SSO flow:

1. Navigate to your login page
2. Click the SSO button
3. Complete the SSO flow with your IdP
4. Verify that you're redirected back to your application and authenticated

## Next steps

> [card] [Customize the UI](https://stytch.com/docs/api-reference/b2b/frontend-sdks/react/prebuilt-ui/login/configuration.md)
>
> Learn how to customize the appearance and behavior of the pre-built UI

> [card] [Configure an IdP](https://stytch.com/docs/multi-tenant-auth/authentication/sso/provider-setup.md)
>
> Add or update SSO Connections for your Identity Provider
