React Quickstart
This quickstart guide covers the essential steps to start integrating Stytch’s Consumer Authentication in a React application.
Overview
Stytch offers developers a React SDK that can be used in client-side React applications. The SDK offers pre-built UI components with customizable login and signup forms, alongside a collection of headless methods for integrating with your own custom UI.
This guide will walk your through initial set up of an Email Magic Links flow login form with our prebuilt UI components.
Want to skip straight to the source code? Check out an example app here.
Getting started
1Install Stytch SDKs and configure your API keys
If you haven't already, create a Stytch Consumer Authentication Project in your Stytch Dashboard.
Add our frontend React SDK package to your React application:
npm install @stytch/react @stytch/vanilla-js --save
Add your Stytch Project's public_token to your application as an environment variable in a .env file:
# .env
# The below values may be found in your Stytch Dashboard: https://stytch.com/dashboard/api-keys
STYTCH_PROJECT_ENV=test
STYTCH_PUBLIC_TOKEN="YOUR_STYTCH_PUBLIC_TOKEN"
2Configure Stytch SDK settings
To allow the Stytch SDK to run on your frontend, you'll also need to:
- Enable frontend SDKs in Test in your Stytch Dashboard here.
- Add the domain your application will run on (e.g. http://localhost:3000) to the list of Domains under Authorized applications.
3Create the UI client and wrap your application in <StytchProvider>
First, initialize the Stytch UI client by invoking the createStytchUIClient constructor function, passing in your Project's public token.
Next, pass the Stytch UI client to the StytchProvider component at the root of your application, making it accessible to all child components.
import { StytchProvider } from '@stytch/react';
import { StytchUIClient } from '@stytch/vanilla-js';
// optional object for configuring SDK cookie behavior, currently showing defaults
const stytchOptions = {
cookieOptions: {
opaqueTokenCookieName: "stytch_session",
jwtCookieName: "stytch_session_jwt",
path: "",
availableToSubdomains: false,
domain: "",
}
}
const stytchClient = new StytchUIClient(
import.meta.env.STYTCH_PUBLIC_TOKEN, // or process.env.STYTCH_PUBLIC_TOKEN for non-Vite based projects
stytchOptions
);
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<StytchProvider stytch={stytchClient}>
<App />
</StytchProvider>
</React.StrictMode>
);
4Add the <StytchLogin> UI component
Create a Login component. You can configure which authentication flow and methods you'd like to offer by modifying the authFlowType and products fields respectively in your config object.
Here's an example of our pre-built UI component utilizing Email Magic Links:
import { StytchLogin } from '@stytch/react';
import { Products } from '@stytch/vanilla-js';
const LoginOrSignup = () => {
const config = {
products: [Products.emailMagicLinks],
emailMagicLinksOptions: {
loginRedirectURL: 'http://localhost:3000/authenticate',
loginExpirationMinutes: 60,
signupRedirectURL: 'http://localhost:3000/authenticate',
signupExpirationMinutes: 60,
},
};
return <StytchLogin config={config} />;
};
For Email Magic Links, you must specify a redirect URL in your Project's Dashboard to authenticate the token. By default, the redirect URL is set to http://localhost:3000/authenticate.
If the <StytchLogin> component is rendered on the redirect URL used for this flow, the Email Magic Link token will automatically be authenticated. You can specify additional Redirect URLs in your Project's Dashboard, and override the default by passing in an explicit emailMagicLinksOptions.loginRedirectURL to your UI config.
You can read more about redirect URLs in this guide.
5What's next
Check out our product-specific guides for how to handle full authentication flows for each product you'd like to support, like Email Magic Links and OAuth.