> ## 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.

# Add login or signup

> Build a login or signup page.

## Quickstart

Select a framework to start:

> [tab] Next.js - App
>
> > [card] [Example app](https://github.com/stytchauth/stytch-all-examples/tree/main/frontend/nextjs/prebuilt/consumer)
> >
> > Next.js code example
>
> > [card] [Web demo](https://demo-money-app.vercel.app/login)
> >
> > Try out the login flow
>
> > [card] [SDK reference](https://stytch.com/docs/api-reference/consumer/frontend-sdks/nextjs/overview.md)
> >
> > Next.js SDK reference
>
> **1. Before you start**
>
> * Have a `public_token` from your Stytch project & environment.
> * Enable and configure [Frontend SDKs](https://stytch.com/dashboard/sdk-configuration) in your Dashboard.
> * Add your application's domain to your project's [Authorized Domains](https://stytch.com/dashboard/sdk-configuration).
>
> **2. Install the Next.js SDK and configure your API key**
>
> 1. In your Next.js app, run the command to install the SDK:
>
> ```bash npm icon=terminal theme={null}
> npm install @stytch/nextjs --save
> ```
>
> 2. Then add your Stytch `public_token` to your project's `.env` file:
>
> ```dotEnv .env icon=file-code theme={null}
> 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.
>
> ```jsx components/stytch-provider.jsx icon=file-code lines theme={null}
> '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>;
> }
> ```
>
> 2. Wrap your application with `<Stytch>` to provide context to all child components:
>
> ```jsx app/layout.jsx icon=file-code lines {1,11} theme={null}
> 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](https://stytch.com/docs/api-reference/consumer/frontend-sdks/react/prebuilt-ui/login/configuration.md).
>
> ```jsx components/LoginOrSignupForm.jsx icon=file-code lines theme={null}
> 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.
>
> ```jsx app/login/page.jsx icon=file-code theme={null}
> 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/>;
> }
> ```
>
> 2. 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`.
>
> **6. Handle session and user data**
>
> At this point, you can retrieve [Session data](https://stytch.com/docs/api-reference/consumer/api/sessions/session-object.md) and [User data](https://stytch.com/docs/api-reference/consumer/api/users/get-user.md) via the SDK. For example, [`useStytchSession()`](https://stytch.com/docs/api-reference/consumer/frontend-sdks/nextjs/hooks/use-stytch-session) lets you easily check for an active session.

> [tab] Next.js - Pages
>
> > [card] [Example app](https://github.com/stytchauth/stytch-all-examples/tree/main/frontend/nextjs/prebuilt/consumer)
> >
> > Next.js code example
>
> > [card] [Web demo](https://demo-money-app.vercel.app/login)
> >
> > Try out the login flow
>
> > [card] [SDK reference](https://stytch.com/docs/api-reference/consumer/frontend-sdks/nextjs/overview.md)
> >
> > Next.js SDK reference
>
> **1. Before you start**
>
> * Have a `public_token` from your Stytch project & environment.
> * Enable and configure [Frontend SDKs](https://stytch.com/dashboard/sdk-configuration) in your Dashboard.
> * Add your application's domain to your project's [Authorized Domains](https://stytch.com/dashboard/sdk-configuration).
>
> **2. Install the Next.js SDK and configure your API key**
>
> 1. In your Next.js app, run the command to install the SDK:
>
> ```bash npm icon=terminal theme={null}
> npm install @stytch/nextjs --save
> ```
>
> 2. Then add your Stytch `public_token` to your project's `.env` file:
>
> ```dotEnv .env icon=file-code theme={null}
> NEXT_PUBLIC_STYTCH_PUBLIC_TOKEN=$PUBLIC_TOKEN
> ```
>
> **3. Wrap your application in \<StytchProvider>**
>
> 1. Initialize the Stytch client through `createStytchClient()`.
> 2. Pass the Stytch UI client to the `<StytchProvider>` component at the root of your application, making it accessible to all child components.
>
> ```jsx pages/_app.jsx icon=file-code lines theme={null}
> // For Page Router setups
> import { createStytchClient, StytchProvider } from '@stytch/nextjs';
>
> const stytch = createStytchClient(process.env.NEXT_PUBLIC_STYTCH_PUBLIC_TOKEN!);
>
> export default function MyApp({ Component, pageProps }: AppProps) {
>   return (
>     <StytchProvider stytch={stytch}>
>       <Component {...pageProps} />
>     </StytchProvider>
>   );
> }
> ```
>
> **4. Add \<StytchLogin> UI component**
>
> 1. Create a `<StytchLogin>` component.
> 2. Set authentication methods and relevant options in the [config object](https://stytch.com/docs/api-reference/consumer/frontend-sdks/react/prebuilt-ui/login/configuration.md).
>
> ```jsx components/LoginOrSignupForm.jsx icon=file-code lines theme={null}
> 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.
>
> ```jsx app/login/page.jsx icon=file-code theme={null}
> 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/>;
> }
> ```
>
> 2. 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`.
>
> **6. Handle session and user data**
>
> At this point, you can retrieve [Session data](https://stytch.com/docs/api-reference/consumer/api/sessions/session-object.md) and [User data](https://stytch.com/docs/api-reference/consumer/api/users/get-user.md) via the SDK. For example, [`useStytchSession()`](https://stytch.com/docs/api-reference/consumer/frontend-sdks/nextjs/hooks/use-stytch-session) lets you easily check for an active session.

> [tab] React
>
> > [card] [Example app](https://github.com/stytchauth/stytch-all-examples/tree/main/frontend/react/prebuilt/consumer)
> >
> > React code example
>
> > [card] [Web demo](https://www.surveyamp.com/login)
> >
> > Try out the login flow
>
> > [card] [SDK reference](https://stytch.com/docs/api-reference/consumer/frontend-sdks/react/overview.md)
> >
> > React SDK reference
>
> **1. Before you start**
>
> * Have a `public_token` from your Stytch project & environment.
> * Enable and configure [Frontend SDKs](https://stytch.com/dashboard/sdk-configuration) in your Dashboard.
> * Add your application's domain to your project's [Authorized Domains](https://stytch.com/dashboard/sdk-configuration).
>
> **2. Install the React SDK and configure your API key**
>
> In your React app, run the command to install the SDK:
>
> ```bash npm icon=terminal theme={null}
> npm install @stytch/react --save
> ```
>
> **3. Wrap your application in \<StytchProvider>**
>
> 1. Initialize the Stytch UI client through `createStytchClient()`.
> 2. Pass the Stytch UI client to the `<StytchProvider>` component at the root of your application, making it accessible to all child components.
>
> ```jsx lines theme={null}
> import { createStytchClient, StytchProvider } from '@stytch/react';
>
> const stytch = createStytchClient($YOUR_PUBLIC_KEY);
>
> // Add the component at the root of your application
> export function App() {
>   return (
>     <StytchProvider stytch={stytch}>
>       {/* Your app code */}
>     </StytchProvider>
>   );
> }
> ```
>
> **4. Add \<StytchLogin> UI component**
>
> 1. Create a `<StytchLogin>` component.
> 2. Set authentication methods and relevant options in the [config object](https://stytch.com/docs/api-reference/consumer/frontend-sdks/react/prebuilt-ui/login/configuration.md).
>
> ```jsx lines theme={null}
> import { Products, StytchLogin } from '@stytch/react';
>
> const config = {
>   products: [Products.emailMagicLinks, Products.oauth],
>   oauthOptions: {
>     providers: [{ type: 'google' }],
>   },
>   sessionOptions: {
>     sessionDurationMinutes: 60,
>   },
> };
>
> const LoginOrSignup = () => {
>   return <StytchLogin config={config} />;
> };
> ```
>
> **5. Add \<LoginOrSignup> to your login page(s)**
>
> 1. Add `<LoginOrSignup>` to your `/login` page:
>
> ```jsx Login.jsx icon=file-code lines theme={null}
> import { useStytchSession } from "@stytch/react";
> import { Navigate } from "react-router";
> import { LoginOrSignup } from "./LoginOrSignup";
>
> export const Login = () => {
>   const { session } = useStytchSession();
>
>   if (session) {
>     return <Navigate to="/home" />;
>   }
>
>   return <LoginOrSignup />;
> };
> ```
>
> 2. If you're using separate routes, add `<LoginOrSignup>` to your `/authenticate` redirect route as well, e.g. `Authenticate.jsx`.
>
> **6. Handle session and user data**
>
> At this point, you can retrieve [Session data](https://stytch.com/docs/api-reference/consumer/api/sessions/session-object.md) and [User data](https://stytch.com/docs/api-reference/consumer/api/users/get-user.md) via the SDK. For example, [`useStytchSession()`](https://stytch.com/docs/api-reference/consumer/frontend-sdks/nextjs/hooks/use-stytch-session) lets you easily check for an active session.

> [tab] Vanilla JS
>
> > [card] [Example app](https://github.com/stytchauth/stytch-all-examples/tree/main/frontend/vanillajs/prebuilt/consumer)
> >
> > Vanilla JS code example
>
> > [card] [Web demo](https://www.surveyamp.com/login)
> >
> > Try out the login flow
>
> > [card] [SDK reference](https://stytch.com/docs/api-reference/consumer/frontend-sdks/vanilla-js/overview.md)
> >
> > Vanilla JS SDK reference
>
> **1. Before you start**
>
> * Have a `public_token` from your Stytch project & environment.
> * Enable and configure [Frontend SDKs](https://stytch.com/dashboard/sdk-configuration) in your Dashboard.
> * Add your application's domain to your project's [Authorized Domains](https://stytch.com/dashboard/sdk-configuration).
>
> **2. Install the JS SDK and configure your API key**
>
> In your project, run the command to install the SDK:
>
> ```bash npm icon=terminal theme={null}
> npm install @stytch/vanilla-js --save
> ```
>
> **3. Initialize the Stytch client for your app**
>
> 1. Initialize the Stytch UI client through `createStytchClient()`.
> 2. Export the initialized client as `stytch` so it can be used across your app.
>
> ```js js/stytch-client.js icon=file-code lines theme={null}
> import { createStytchClient } from "@stytch/vanilla-js";
> export const stytch = createStytchClient($YOUR_PUBLIC_KEY);
> ```
>
> **4. Register the Stytch UI web component and configure it**
>
> 1. Create a `login.js` file to register and initialize the custom element.
> 2. Set authentication methods and relevant options in the [config object](https://stytch.com/docs/api-reference/consumer/frontend-sdks/vanilla-js/prebuilt-ui/login/configuration.md).
>
> ```js js/login.js icon=file-code lines theme={null}
> import { Products, StytchUI } from '@stytch/vanilla-js';
> import { stytch } from "./stytch-client.js";
>
> // Register the custom element
> customElements.define('stytch-ui', StytchUI);
>
> // Pass in the StytchClient and config
> const login = document.getElementById('stytch-ui');
> login.render({
>   client: stytch,
>   config: {
>     products: [Products.emailMagicLinks, Products.oauth],
>     oauthOptions: {
>       providers: [{ type: 'google' }],
>     },
>     sessionOptions: {
>       sessionDurationMinutes: 60,
>     },
>   },
> });
> ```
>
> **5. Add Stytch UI on your login page(s)**
>
> 1. Add `<stytch-ui>` on your `/login` page:
>
> ```html login.html {5,6} icon=file-code lines theme={null}
> <!DOCTYPE html>
> <html lang="en">
>   <head>...</head>
>   <body>
>     <stytch-ui id="stytch-ui" />
>     <script type="module" src="js/login.js"></script>
>   </body>
> </html>
> ```
>
> 2. If you're using separate routes, add `<stytch-ui>` on your `/authenticate` redirect route as well, e.g. `authenticate.html`.
>
> **6. Handle session and user data**
>
> At this point, you can retrieve [Session data](https://stytch.com/docs/api-reference/consumer/api/sessions/session-object.md) and [User data](https://stytch.com/docs/api-reference/consumer/api/users/get-user.md) via the SDK. For example, [`useStytchSession()`](https://stytch.com/docs/api-reference/consumer/frontend-sdks/react/hooks/use-stytch-session) lets you retrieve the current session and can be used to determine if a user is already logged in.

## Next steps

* Authentication methods
* Session management
