This example application demonstrates how one may use Stytch within a Next.js 13 application using the new App Router. If you'd like to see an example of Stytch with Next.js's Page Router, you can find it here.
In Next.js 13's App Router, you may use both Client and Server components. This example app primarily uses Client components, however you can see an example of a Server component in /src/components/Authenticate.js
. Our Next.js SDK is compatible with Client components, so anywhere you use it, ensure that you include 'use client'
at the top of the component. If you'd like to use Server components, you may use our Node Backend SDK to power your authentication flow.
This application features Email Magic Links and Google OAuth authentication. You can use this application's source code as a learning resource, or use it as a jumping off point for your own project. We are excited to see what you build with Stytch!
Follow the steps below to get this application fully functional and running using your own Stytch credentials.
-
Create a Stytch account. Once your account is set up a Project called "My first project" will be automatically created for you.
-
Within your new Project, navigate to SDK configuration, and click Enable SDK.
-
Navigate to OAuth, and enable login for Google in the Test environment. Config will be done for you automatically in Test.
-
Finally, navigate to API Keys. You will need the
project_id
,secret
, andpublic_token
values found on this page later on.
In your terminal clone the project and install dependencies:
git clone https://github.com/cal-stytch/stytch-nextjs13-example.git
cd stytch-nextjs13-example
# Install dependencies, you may use either pnpm or npm.
pnpm i
# or
npm i
Next, create .env.local
file by running the command below which copies the contents of .env.template
.
cp .env.template .env.local
Open .env.local
in the text editor of your choice, and set the environment variables using the project_id
, secret
, and public_token
found on API Keys. Leave the STYTCH_PROJECT_ENV
value as test
.
# This is what a completed .env.local file will look like
STYTCH_PROJECT_ENV=test
STYTCH_PROJECT_ID=project-test-00000000-0000-1234-abcd-abcdef1234
NEXT_PUBLIC_STYTCH_PUBLIC_TOKEN=public-token-test-abcd123-0000-0000-abcd-1234567abc
STYTCH_SECRET=secret-test-12345678901234567890abcdabcd
After completing all the set up steps above the application can be run with the command:
pnpm run dev
# or
npm run dev
The application will be available at http://localhost:3000
.
You'll be able to login with Email Magic Links or Google OAuth and see your Stytch User object, Stytch Session, and see how logging out works.
This example app showcases a small portion of what you can accomplish with Stytch. Here are a few ideas to explore:
- Add additional login methods like Passwords.
- Replace the prebuilt UI with your own using by using the SDK's headless methods.
- Replace the Google OAuth button with the high converting Google One Tap UI.
- Secure your app further by building MFA authentication using methods like WebAuthn.``
Join the discussion, ask questions, and suggest new features in our Slack community!
Check out the Stytch Forum or email us at support@stytch.com.