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

<Tabs>
  <Tab title="Next.js - App" icon="triangle">
    <Columns cols={3}>
      <Card title="Example app" icon="github" href="https://github.com/stytchauth/stytch-all-examples/tree/main/frontend/nextjs/prebuilt/consumer">
        Next.js code example
      </Card>

      <Card title="Web demo" icon="compass" href="https://demo-money-app.vercel.app/login">
        Try out the login flow
      </Card>

      <Card title="SDK reference" icon="book-text" href="/api-reference/consumer/frontend-sdks/nextjs/overview">
        Next.js SDK reference
      </Card>
    </Columns>

    <Steps>
      <Step title="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).
      </Step>

      <Step title="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
        ```
      </Step>

      <Step title="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>
          );
        }
        ```
      </Step>

      <Step title="Add <StytchLogin> UI component">
        1. Create a `<StytchLogin>` component.
        2. Set authentication methods and relevant options in the [config object](/api-reference/consumer/frontend-sdks/react/prebuilt-ui/login/configuration).

        ```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} />;
        };
        ```
      </Step>

      <Step title="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`.
      </Step>

      <Step title="Handle session and user data" icon="check">
        At this point, you can retrieve [Session data](/api-reference/consumer/api/sessions/session-object) and [User data](/api-reference/consumer/api/users/get-user) via the SDK. For example, [`useStytchSession()`](/api-reference/consumer/frontend-sdks/nextjs/hooks/use-stytch-session) lets you easily check for an active session.
      </Step>
    </Steps>
  </Tab>

  <Tab title="Next.js - Pages" icon="triangle">
    <Columns cols={3}>
      <Card title="Example app" icon="github" href="https://github.com/stytchauth/stytch-all-examples/tree/main/frontend/nextjs/prebuilt/consumer">
        Next.js code example
      </Card>

      <Card title="Web demo" icon="compass" href="https://demo-money-app.vercel.app/login">
        Try out the login flow
      </Card>

      <Card title="SDK reference" icon="book-text" href="/api-reference/consumer/frontend-sdks/nextjs/overview">
        Next.js SDK reference
      </Card>
    </Columns>

    <Steps>
      <Step title="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).
      </Step>

      <Step title="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
        ```
      </Step>

      <Step title="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>
          );
        }
        ```
      </Step>

      <Step title="Add <StytchLogin> UI component">
        1. Create a `<StytchLogin>` component.
        2. Set authentication methods and relevant options in the [config object](/api-reference/consumer/frontend-sdks/react/prebuilt-ui/login/configuration).

        ```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} />;
        };
        ```
      </Step>

      <Step title="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`.
      </Step>

      <Step title="Handle session and user data" icon="check">
        At this point, you can retrieve [Session data](/api-reference/consumer/api/sessions/session-object) and [User data](/api-reference/consumer/api/users/get-user) via the SDK. For example, [`useStytchSession()`](/api-reference/consumer/frontend-sdks/nextjs/hooks/use-stytch-session) lets you easily check for an active session.
      </Step>
    </Steps>
  </Tab>

  <Tab title="React" icon="react">
    <Columns cols={3}>
      <Card title="Example app" icon="github" href="https://github.com/stytchauth/stytch-all-examples/tree/main/frontend/react/prebuilt/consumer">
        React code example
      </Card>

      <Card title="Web demo" icon="compass" href="https://www.surveyamp.com/login">
        Try out the login flow
      </Card>

      <Card title="SDK reference" icon="book-text" href="/api-reference/consumer/frontend-sdks/react/overview">
        React SDK reference
      </Card>
    </Columns>

    <Steps>
      <Step title="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).
      </Step>

      <Step title="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
        ```
      </Step>

      <Step title="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>
          );
        }
        ```
      </Step>

      <Step title="Add <StytchLogin> UI component">
        1. Create a `<StytchLogin>` component.
        2. Set authentication methods and relevant options in the [config object](/api-reference/consumer/frontend-sdks/react/prebuilt-ui/login/configuration).

        ```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} />;
        };
        ```
      </Step>

      <Step title="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`.
      </Step>

      <Step title="Handle session and user data" icon="check">
        At this point, you can retrieve [Session data](/api-reference/consumer/api/sessions/session-object) and [User data](/api-reference/consumer/api/users/get-user) via the SDK. For example, [`useStytchSession()`](/api-reference/consumer/frontend-sdks/nextjs/hooks/use-stytch-session) lets you easily check for an active session.
      </Step>
    </Steps>
  </Tab>

  <Tab title="Vanilla JS" icon="square-js">
    <Columns cols={3}>
      <Card title="Example app" icon="github" href="https://github.com/stytchauth/stytch-all-examples/tree/main/frontend/vanillajs/prebuilt/consumer">
        Vanilla JS code example
      </Card>

      <Card title="Web demo" icon="compass" href="https://www.surveyamp.com/login">
        Try out the login flow
      </Card>

      <Card title="SDK reference" icon="book-text" href="/api-reference/consumer/frontend-sdks/vanilla-js/overview">
        Vanilla JS SDK reference
      </Card>
    </Columns>

    <Steps>
      <Step title="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).
      </Step>

      <Step title="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
        ```
      </Step>

      <Step title="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);
        ```
      </Step>

      <Step title="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](/api-reference/consumer/frontend-sdks/vanilla-js/prebuilt-ui/login/configuration).

        ```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,
            },
          },
        });
        ```
      </Step>

      <Step title="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`.
      </Step>

      <Step title="Handle session and user data" icon="check">
        At this point, you can retrieve [Session data](/api-reference/consumer/api/sessions/session-object) and [User data](/api-reference/consumer/api/users/get-user) via the SDK. For example, [`useStytchSession()`](/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.
      </Step>
    </Steps>
  </Tab>
</Tabs>

## Next steps

* Authentication methods
* Session management
