Quickstart
Select a framework to start:- Next.js - App
- Next.js - Pages
- React
- Vanilla JS
Example app
Next.js code example
Web demo
Try out the login flow
SDK reference
Next.js SDK reference
Before you start
- Have a
public_tokenfrom your Stytch project & environment. - Enable and configure Frontend SDKs in your Dashboard.
- Add your application’s domain to your project’s Authorized Domains.
Install the Next.js SDK and configure your API key
- In your Next.js app, run the command to install the SDK:
npm
- Then add your Stytch
public_tokento your project’s.envfile:
.env
Set up a Stytch context provider
- Create a custom
Stytchwrapper component that initializes the Stytch client.
components/stytch-provider.tsx
- Wrap your application with
<Stytch>to provide context to all child components:
app/layout.tsx
Add <StytchB2B> UI component
- Create a
<StytchB2B>component. - Set authentication methods and relevant options in the config object.
components/LoginOrSignupDiscoveryForm.tsx
Set redirect URLs, if necessary
Point
discoveryRedirectURL to the URL in your app where users are redirected back to after clicking on their email magic link or after completing OAuth:- Using a single route: Set the redirect back to the
/loginroute if you plan on having one shared experience for both the start (login or signup) and end (MFA, organization discovery) of the authentication flow. - Using separate routes: Set the redirect to a new route (e.g.
/authenticate) if you want a different user experience between the start and the end of the authentication flow. For example, a marketing-oriented login page that redirects to a more focused authentication experience for MFA and organization selection.
For security, add redirect URLs in the Dashboard before being used.
Add <LoginOrSignupDiscoveryForm> to your login page(s)
- Add
<LoginOrSignupDiscoveryForm>to your/loginpage.
app/login/page.tsx
- If you’re using separate routes, add
<LoginOrSignupDiscoveryForm>to your/authenticateredirect route as well, e.g.app/authenticate/page.tsxorpages/authenticate.tsx.
Handle session and member data
After a user selects an organization, the
<StytchB2B> component automatically exchanges their intermediate_session_token for a fully minted session_token and session_jwt, both of which are stored in browser cookies.At this point, you can retrieve Session data and Member data via the SDK. For example, useStytchMemberSession() lets you easily check for an active session.Next steps
- Authentication methods
- Session management