B2B Passwords Prebuilt UI Integration
Discovery Flow
In Stytch’s B2B Passwords Discovery flow, end users authenticate before selecting the Organization they want to access by signing in through your application's centralized login page.
This flow is typically used with Cross-Organization passwords, where a single password linked to the user’s email address grants access to any Organization they belong to that has Passwords enabled as an allowed authentication method. This guide walks through how to enable password authentication using Stytch’s frontend SDK with our pre-built UI component. The pre-built UI component handles all password-based flows, including login and signup, password resets, and Organization creation in a seamless, integrated experience.
Steps
1Complete config steps
If you haven't done so already:
- Set up Redirect URLs in the Stytch Dashboard for:
- Signup, Login, Invite, Reset password and Discovery types
- Enable the Frontend SDKs in your Stytch Dashboard
- Enable Cross-Organization passwords by toggling on “Allow Passwords to be used between Organizations” in your Password strength configuration
2Configure the B2B UI Component
To display Passwords as a login option in the UI, add it to the products array in your UI config and set the authFlowType to “Discovery”.
You can set your desired loginRedirectURL and resetPasswordRedirectURL values within the passwordOptions object. The resetPasswordRedirectURL will be used when the user selects the “Reset Password” button in password reset emails, and the loginRedirectURL will be used when the user selects the “Log in without password” button in password reset emails.
We recommend reviewing the UI Config documentation if you want to explore available styling options.
Most developers choose to place this configuration in a helper file. In this guide we’re doing the same, and you can find this pattern in the companion example app here, but you can alternatively include the config options directly within your Login component.
config.ts
import { AuthFlowType, B2BProducts } from "@stytch/vanilla-js";
export const discoveryConfig = {
products: [B2BProducts.passwords],
sessionOptions: { sessionDurationMinutes: 60 },
passwordOptions: {
loginRedirectURL: "http://localhost:3000/authenticate",
resetPasswordRedirectURL: "http://localhost:3000/passwordreset",
resetPasswordExpirationMinutes: 20
},
authFlowType: AuthFlowType.Discovery,
};
export const discoveryStyles = {
fontFamily: 'Arial',
container: {
width: '500px',
},
}
3Create the Login UI Component
The example below shows how to create a Login component. It applies the configuration and style defined in your config file. This component automatically handles the UI events and calls the Stytch API with the necessary authentication data. You can then import the Login component into your main application file:
Login.tsx
import { StytchB2B } from "@stytch/react/b2b";
import { discoveryConfig, discoveryStyles } from './config';
export const Login = () => {
return (
<div className="centered-login">
<StytchB2B
config={discoveryConfig}
styles={discoveryStyles}
/>
</div>
);
};
4Render the Login component
In this example, there are three routes defined, all of which render the Login component. The /authenticate and /passwordreset routes are the login and password reset redirect URLs defined in the UI configuration.
App.tsx
import { Routes, Route } from "react-router-dom";
import { Login } from "./Login";
export const App = () => {
return (
<Routes>
<Route path="/" element={<Login />} />
<Route path="/authenticate" element={<Login />}/>
<Route path="/passwordreset" element={<Login />}/>
</Routes>
);
};
A new user can sign up by clicking the ‘Sign up or reset password’ link
A new user is prompted to enter their email. This triggers a verification email containing a magic link. The link includes a unique token that validates the user and allows them to create a password.
The verification email can be customized in the Email Templates section of your Dashboard as the Passwords > Email verification template type.
Upon clicking the ‘Reset Password’ button, a user is redirected to the Password Reset redirect url, where they can enter a new password
The user is prompted to select the Organization they want to access. If the user does not have access to any Organizations, they can create a new one by clicking the ‘Create an organization’ button.
This setting can be configured in the Frontend SDK settings in the Stytch Dashboard, as well as in your UI configurations. Note that the Dashboard settings will take precedence over the UI configurations. The email address is redacted in the image below.
If a new user does not have access to any Organizations and the ‘Create Organization’ method is not enabled, the user is advised to reach out to an administrator.
A returning user can log in with their email and password
The user is prompted to select the Organization they want to access.
The user is now successfully logged in.
The user can choose to reset their password by clicking the “Sign up or reset password” link.
The user is prompted to enter their email. This triggers a password reset email containing a magic link. The link includes a unique token that validates the user and allows them to reset their password.
The Password Reset email can be customized in the Email Templates section of your Dashboard as the Passwords > Password reset template type.
Upon clicking the ‘Reset Password’ button, the user is redirected to the Password Reset redirect url, where they can enter a new password.
The user is prompted to select the Organization they want to access.
The user is now successfully logged in