Next.js Quickstart
This tutorial adds Keyward sign-in to a new Next.js app using the App Router.
Before you start: you need a Keyward application. Create one in the dashboard — the free plan covers 10,000 monthly users.
- Install @keyward/nextjs
- npm
- pnpm
- yarn
npm install @keyward/nextjspnpm add @keyward/nextjsyarn add @keyward/nextjs - Set your keys
Add your publishable and secret keys to
.env.local. Never commit the secret key..env.local NEXT_PUBLIC_KEYWARD_PUBLISHABLE_KEY=pk_test_•••• KEYWARD_SECRET_KEY=sk_test_•••• - Add the proxy
proxy.ts import { keywardProxy } from "@keyward/nextjs/server"; export default keywardProxy(); export const config = { matcher: ["/((?!_next|.*\\..*).*)", "/(api|trpc)(.*)"], }; - Wrap your app
app/layout.tsx import { KeywardProvider, SignedIn, SignedOut, SignInButton, UserMenu } from "@keyward/nextjs"; export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <KeywardProvider> <html lang="en"> <body> <header> <SignedOut><SignInButton /></SignedOut> <SignedIn><UserMenu /></SignedIn> </header> {children} </body> </html> </KeywardProvider> ); } - Run it
npm run devOpen
localhost:3000, select Sign in, and create the first user.
Show the user menu
The same component ships for every SDK; pick yours in the sidebar and this sample follows.
- Next.js
- React
- Expo
import { UserMenu } from "@keyward/nextjs";
export default function Nav() { return <UserMenu afterSignOutUrl="/" />; }import { UserMenu } from "@keyward/react";
export function Nav() { return <UserMenu afterSignOutUrl="/" />; }import { UserMenu } from "@keyward/expo";
export function Nav() { return <UserMenu afterSignOutUrl="/" />; }Next steps
Guide | What you will learn |
|---|---|
Require a session on a page or an API route | |
Use | |
Change colours, fonts and layout |
Updated
What did you think of this content?