Skip to content
exwave.Docs

Auth with Next.js

This page explains how Exwave Auth works in a Next.js 16 app with the App Router: where the session is kept, the files that set it up, and how to read the user on the server.

If you made your app with npm create exwave, or added @exwave/auth-nextjs with the shadcn CLI, these files are already in your app. See Get started to set them up.

With Next.js, your server talks to Exwave Auth, not the browser. The session is kept in an encrypted cookie. The cookie is httpOnly, so JavaScript in the page can’t read it. The refresh token, which renews the session, never reaches the browser.

Install the three packages:

Terminal window
npm install @exwave-nexus/auth @exwave-nexus/auth-react @exwave-nexus/auth-nextjs

@exwave-nexus/auth-nextjs has the server code, and a provider that uses the Next.js router. The hooks and components come from @exwave-nexus/auth-react.

This creates the exwave object that the other files use:

lib/exwave.ts
import { createExwaveNext } from "@exwave-nexus/auth-nextjs";
export const exwave = createExwaveNext();

It reads two environment variables, from .env.local while you develop:

Variable Value
NEXT_PUBLIC_EXWAVE_PUBLISHABLE_KEY Your publishable key, from Nexus → Auth.
EXWAVE_COOKIE_SECRET A random secret of at least 32 characters, which encrypts the session cookie. Use a different one in each environment.

It reads them the first time it’s used, not when the app is built. So you can build the app without them.

The screens don’t call Exwave directly. They call these routes on your own server, which call Exwave and set the cookie:

app/api/exwave/[...action]/route.ts
import { exwave } from "@/lib/exwave";
export const { GET, POST } = exwave.routeHandlers();

The routes handle sign-up, sign-in, email verification, password reset, sign-out, the session, your platform’s Auth settings and player access tokens. They only answer requests from your own site.

Next.js runs proxy.ts before each page. (Next.js 16 calls this file proxy; it used to be called middleware.)

proxy.ts
import type { NextRequest } from "next/server";
import { exwave } from "@/lib/exwave";
export function proxy(request: NextRequest) {
return exwave.proxy(request);
}
export const config = {
matcher: ["/((?!api/exwave|_next/static|_next/image|favicon.ico).*)"],
};

It renews sessions before your pages render. It also clears sessions that ended elsewhere, for example when someone signed out on every device or their account was locked. The sign-in page can then say why. The matcher skips the Exwave routes and static files.

In your root layout, wrap {children} in <ExwaveProvider>. The full file is in Get started, step 4.

app/layout.tsx
import { ExwaveProvider } from "@exwave-nexus/auth-nextjs/client";
<ExwaveProvider initialState={await exwave.getAuthState()}>
{children}
</ExwaveProvider>

initialState is the session as the server sees it, without any tokens. With it, the first render already knows who is signed in, so the page doesn’t show a signed-out state first. The provider changes pages with the Next.js router, and refreshes Server Components when someone signs in or out.

Call Use it in What it gives you
exwave.getUser() Any server code The signed-in user, or null.
exwave.getAuthState() Your root layout The session to pass as initialState.
exwave.getSignOutReason() Your sign-in page Why the last session ended, for 5 minutes after it ended.
exwave.signIn(), signUp(), signOut() and the others Server Functions The same actions as the screens. They also set the cookie.
exwave.getPlayerAccessToken() Server Functions and Route Handlers A token for your game. See Games.

This page sends people who aren’t signed in to the sign-in page. After they sign in, <SignInForm /> sends them back to the ?redirect= path, if it’s a path on your site.

app/account/page.tsx
import { redirect } from "next/navigation";
import { exwave } from "@/lib/exwave";
export default async function AccountPage() {
const user = await exwave.getUser();
if (!user) redirect("/sign-in?redirect=/account");
return <h1>{user.displayName}</h1>;
}

Verification and password reset emails link to /verify and /reset-password on your site. If those pages are somewhere else, change the paths in lib/exwave.ts:

lib/exwave.ts
export const exwave = createExwaveNext({
paths: { verifyEmail: "/auth/verify", resetPassword: "/auth/reset-password" },
});

The links start with the address people used to reach your app. If your app runs behind a proxy that changes the host name, set the public address yourself:

lib/exwave.ts
export const exwave = createExwaveNext({ appUrl: "https://example.com" });

Either way, that address must be your Site URL or one of your Redirect URLs, in Nexus → Auth → Settings.