Skip to main content

Quickstart

Select a framework to start:
1

Before you start

  • Have a public_token from your Stytch project & environment.
  • Enable and configure Frontend SDKs in your Dashboard.
  • Add your application’s domain to your project’s Authorized Domains.
2

Install the Next.js SDK and configure your API key

  1. In your Next.js app, run the command to install the SDK:
npm
npm install @stytch/nextjs --save
  1. Then add your Stytch public_token to your project’s .env file:
.env
NEXT_PUBLIC_STYTCH_PUBLIC_TOKEN=$PUBLIC_TOKEN
3

Set up a Stytch context provider

  1. Create a custom Stytch wrapper component that initializes the Stytch client.
components/stytch-provider.jsx
'use client';

import { ReactNode } from 'react';
import { createStytchClient, StytchProvider } from '@stytch/nextjs';

const stytchClient = createStytchClient(
  process.env.NEXT_PUBLIC_STYTCH_PUBLIC_TOKEN
);

export default function Stytch({ children }: { children: ReactNode }) {
  return <StytchProvider stytch={stytchClient}>{children}</StytchProvider>;
}
  1. Wrap your application with <Stytch> to provide context to all child components:
app/layout.jsx
import Stytch from './components/stytch-provider';

export default function RootLayout({
  children,
}: {
  children: ReactNode
}) {
  return (
    <html>
      <body>
        <Stytch>{children}</Stytch>
      </body>
    </html>
  );
}
4

Add <StytchLogin> UI component

  1. Create a <StytchLogin> component.
  2. Set authentication methods and relevant options in the config object.
components/LoginOrSignupForm.jsx
import { Products, StytchLogin } from '@stytch/nextjs';

const config = {
  products: [Products.emailMagicLinks, Products.oauth],
  oauthOptions: {
    providers: [{ type: 'google' }],
  },
  sessionOptions: {
    sessionDurationMinutes: 60,
  },
};

export const LoginOrSignupForm = () => {
  return <StytchLogin config={config} />;
};
5

Add <LoginOrSignupForm> to your login page(s)

  1. Add <LoginOrSignupForm> to your /login page.
app/login/page.jsx
import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useStytchSession } from '@stytch/nextjs';
import { LoginOrSignupForm } from "src/components/LoginOrSignupForm";

export default function Login() {
  const { session, isInitialized } = useStytchSession();
  const router = useRouter();

  useEffect(() => {
    if (session && isInitialized) {
      router.replace("/home");
    }
  }, [session, isInitialized, router]);

  if (!isInitialized || session) {
    return <p>Loading...</p>;
  }

  return <LoginOrSignupForm/>;
}
  1. If you’re using separate routes, add <LoginOrSignupForm> to your /authenticate redirect route as well, e.g. app/authenticate/page.jsx or pages/authenticate.jsx.

Handle session and user data

At this point, you can retrieve Session data and User data via the SDK. For example, useStytchSession() lets you easily check for an active session.

Next steps

  • Authentication methods
  • Session management