Connecting your sign-in
Morph saves a page per signed-in user, so it needs to know who's signed in. It never sees your users' passwords or sessions: your server tells it a user id, and gets back a short-lived pass (a token) for that user's browser.
browser ──POST /api/morph-token──▶ your server ──secret key + user id──▶ Morph
◀───────────── token (1 hour, this user only) ◀──────────────────────┘
Your secret key stays on your server. The browser only ever holds the token.
The token endpoint#
npm init usemorph@latest writes app/api/morph-token/route.ts. It's one function: return the signed-in user, or null.
import { morphTokenHandler } from "@usemorph/node";
export const POST = morphTokenHandler({
getUser: async (req) => {
const user = /* your app's signed-in user */;
return user ? { id: user.id, name: user.name } : null;
},
});
idis required: a stable id that never changes for that user. Morph saves their pages under it. (Avoid emails, which can change.)- Anything else you return (
name,role,plan…) is passed to the AI as context, e.g. to greet the user by name. Never return secrets or customer data. - Returning
nullanswers "not signed in": the page simply stays as you built it.
init wires this up for you when it recognizes your sign-in: Clerk, Auth.js (NextAuth), or your own exported currentUser() / getCurrentUser() / getUser() / getSession() that returns a user with an id. Otherwise it leaves a TODO(morph) line to fill in with one of the recipes below.
Recipes#
Clerk#
import { auth } from "@clerk/nextjs/server";
getUser: async () => {
const { userId } = await auth();
return userId ? { id: userId } : null;
},
Auth.js (NextAuth v5)#
import { auth } from "@/auth";
getUser: async () => {
const session = await auth();
const id = session?.user?.id; // add the id to the session in your callbacks
return id ? { id, name: session.user?.name ?? undefined } : null;
},
Supabase#
import { createClient } from "@/utils/supabase/server"; // your server client helper
getUser: async () => {
const supabase = await createClient();
const { data } = await supabase.auth.getUser();
return data.user ? { id: data.user.id } : null;
},
Firebase#
With Firebase, sign-in usually lives in the browser. Two common setups:
You use session cookies (firebase-admin createSessionCookie): read the cookie on the server.
import { cookies } from "next/headers";
import { getAuth } from "firebase-admin/auth";
getUser: async () => {
const cookie = (await cookies()).get("session")?.value;
if (!cookie) return null;
try {
const decoded = await getAuth().verifySessionCookie(cookie, true);
return { id: decoded.uid, name: decoded.name };
} catch {
return null;
}
},
Sign-in only in the browser: send the user's Firebase ID token with the request, and verify it on the server.
// where you render <MorphProvider>
"use client";
import { getAuth } from "firebase/auth";
<MorphProvider
getToken={async () => {
const idToken = await getAuth().currentUser?.getIdToken();
const res = await fetch("/api/morph-token", {
method: "POST",
headers: idToken ? { Authorization: `Bearer ${idToken}` } : {},
});
if (!res.ok) throw new Error("Not signed in");
return res.json();
}}
>
// app/api/morph-token/route.ts
import { getAuth } from "firebase-admin/auth";
getUser: async (req) => {
const idToken = req.headers.get("authorization")?.replace(/^Bearer /, "");
if (!idToken) return null;
try {
const decoded = await getAuth().verifyIdToken(idToken);
return { id: decoded.uid, name: decoded.name };
} catch {
return null;
}
},
Your own sign-in#
Anything that can answer "who is this request from?" works: read your session cookie, look up the user, return { id }.
Other servers#
Express:
import { morphExpressHandler } from "@usemorph/node";
app.post("/api/morph-token", morphExpressHandler({
getUser: (req) => req.user && { id: req.user.id },
}));
Anything else: call createMorphSession and return its result as JSON.
import { createMorphSession } from "@usemorph/node";
const session = await createMorphSession({ userId: user.id, attributes: { name: user.name } });
// → { token, expiresAt, serviceUrl }: send this to the browser
If your endpoint isn't at /api/morph-token, tell the provider: <MorphProvider tokenUrl="/morph/token">.
Environment variables#
MORPH_SECRET_KEY |
Required. Your app's secret key (init puts it in .env.local). Server only: never expose it to the browser, never prefix it with NEXT_PUBLIC_. |
MORPH_URL |
Optional. Only for a self-hosted or local Morph; defaults to https://api.usemorph.dev. |