NodeOps Auth Setup
Add NodeOps PKCE OAuth to an existing Next.js (App Router) project.
Prerequisites
Before starting, verify the project is compatible:
- App Router required: Check if
app/directory exists. If onlypages/exists, stop and tell the user: "This skill only supports Next.js App Router. Your project uses Pages Router." - Next.js required: Check
package.jsonfornextin dependencies. If missing, stop and tell the user this is a Next.js-only package.
Idempotency
Before each step, check if the work is already done. Skip steps that are already complete:
- If
@nodeops-createos/integration-oauthis already inpackage.jsondependencies, skip install. - If
app/api/auth/me/route.tsandapp/api/auth/token/route.tsalready exist, skip route creation. - If
AuthProviderfrom@nodeops-createos/integration-oauthis already imported in the layout, skip wrapping. - If
app/callback/page.tsx(or.jsx) already exists, skip callback page creation. - If
.env.examplealready exists and containsNODEOPS_, skip env file creation.
Steps
-
Detect project setup — read
package.jsonand check for lock files:- Package manager: if
pnpm-lock.yamlexists → pnpm, ifyarn.lockexists → yarn, else → npm - TypeScript: if
tsconfig.jsonexists use.ts/.tsxextensions, else.js/.jsx - Confirm
app/directory exists (App Router). If onlypages/exists, stop. - Confirm
nextis in dependencies. If not, stop.
- Package manager: if
-
Install the package — run the appropriate install command:
- npm:
npm install @nodeops-createos/integration-oauth - pnpm:
pnpm add @nodeops-createos/integration-oauth - yarn:
yarn add @nodeops-createos/integration-oauth - If install fails: check if the user has network access and the registry is reachable. Suggest running the command manually if it keeps failing.
- npm:
-
Create the API routes — create two separate route files, creating directories as needed:
app/api/auth/me/route.ts(or.js):export { GET } from '@nodeops-createos/integration-oauth/server/me';app/api/auth/token/route.ts(or.js):export { POST } from '@nodeops-createos/integration-oauth/server/token'; -
Wrap layout with AuthProvider — read the root layout file. It could be
app/layout.tsx,app/layout.jsx,app/layout.js, orapp/layout.ts. Find whichever exists.- Add the import at the top of the file:
import { AuthProvider } from '@nodeops-createos/integration-oauth'; - Find
{children}inside the<body>tag. Wrap it with<AuthProvider>:<AuthProvider>{children}</AuthProvider> - Do NOT add "use client" to the layout.
AuthProvideris already marked"use client"internally — it works fine when imported from a Server Component layout. - If the layout already has other providers (e.g., ThemeProvider, QueryClientProvider), nest
<AuthProvider>alongside them. Do NOT remove or replace existing providers. - If
{children}is not directly inside<body>(e.g., it's inside a wrapper div or fragment), wrap wherever{children}appears. - If no layout file exists, stop and tell the user to create a root layout first.
- Add the import at the top of the file:
-
Create the callback page — ask the user: "Where should users be redirected after login? (default: /dashboard)". Use their answer as the
redirectTovalue. Then createapp/callback/page.tsx(or.jsx):"use client"; import { useCallbackHandler } from "@nodeops-createos/integration-oauth"; export default function Callback() { const { loading, error } = useCallbackHandler({ redirectTo: "/dashboard" }); if (loading) return ( <div style={{ display: "flex", justifyContent: "center", alignItems: "center", height: "100vh" }}> <p>Signing in...</p> </div> ); if (error) return <p>Login failed: {error}</p>; return null; } -
Create
.env.example— create this file at the project root:# ── Client-side (exposed to browser via NEXT_PUBLIC_ prefix) ────────────────── NEXT_PUBLIC_NODEOPS_AUTH_URL=https://id.nodeops.network/oauth2/auth NEXT_PUBLIC_NODEOPS_CLIENT_ID=your_client_id_here NEXT_PUBLIC_NODEOPS_REDIRECT_URI=http://localhost:3000/callback NEXT_PUBLIC_NODEOPS_SCOPES=offline_access offline openid # ── Server-side only (never sent to browser) ────────────────────────────────── NODEOPS_CLIENT_SECRET=your_client_secret_here NODEOPS_TOKEN_URL=https://id.nodeops.network/oauth2/token NODEOPS_USERINFO_URL=https://autogen-v2-api.nodeops.network/v1/users/me -
Create
.env.local— if.env.localdoes not already exist, copy.env.exampleto.env.localand remind the user to fill inNEXT_PUBLIC_NODEOPS_CLIENT_IDandNODEOPS_CLIENT_SECRETwith their credentials from the NodeOps developer portal. If.env.localalready exists, append the missingNODEOPS_vars only — do NOT overwrite existing values. -
Check
.gitignore— read.gitignore(if it exists). If.env.localis NOT listed, add it to prevent leaking secrets. Also ensure.envis listed. If no.gitignoreexists, create one with at least:.env .env.local -
Docker/container deployment — check if a
Dockerfileexists in the project root. If it does, verify it handles NodeOps env vars correctly and warn the user if not. If the user asks about Docker or deployment, create or update the Dockerfile.Critical:
NEXT_PUBLIC_*vars are baked into the JS bundle at build time by Next.js. They must be set asENVorARGin the Dockerfile beforeRUN npm run build. Setting them only at runtime does nothing — the bundle will containundefined.The two categories of vars:
Variable When needed Why NEXT_PUBLIC_NODEOPS_AUTH_URLBuild time Inlined into client JS bundle NEXT_PUBLIC_NODEOPS_CLIENT_IDBuild time Inlined into client JS bundle NEXT_PUBLIC_NODEOPS_REDIRECT_URIBuild time Inlined into client JS bundle NEXT_PUBLIC_NODEOPS_SCOPESBuild time Inlined into client JS bundle NODEOPS_CLIENT_SECRETRuntime only Read by API route on each request NODEOPS_TOKEN_URLRuntime only Read by API route on each request NODEOPS_USERINFO_URLRuntime only Read by API route on each request CreateOS note: CreateOS uses the Dockerfile when one is present. Use a single-stage Dockerfile only — multi-stage builds with
COPY --from=builderfail on this platform. Remove the Dockerfile entirely to fall back to CreateOS's default install/run commands. See the CreateOS section (step 10) for the runtime config workaround forNEXT_PUBLIC_*vars.Example Dockerfile (single-stage, compatible with CreateOS):
FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . # NEXT_PUBLIC_* must be present at build time — they get baked into the JS bundle ARG NEXT_PUBLIC_NODEOPS_AUTH_URL=https://id.nodeops.network/oauth2/auth ARG NEXT_PUBLIC_NODEOPS_CLIENT_ID ARG NEXT_PUBLIC_NODEOPS_REDIRECT_URI ARG NEXT_PUBLIC_NODEOPS_SCOPES=offline_access offline openid ENV NEXT_PUBLIC_NODEOPS_AUTH_URL=$NEXT_PUBLIC_NODEOPS_AUTH_URL ENV NEXT_PUBLIC_NODEOPS_CLIENT_ID=$NEXT_PUBLIC_NODEOPS_CLIENT_ID ENV NEXT_PUBLIC_NODEOPS_REDIRECT_URI=$NEXT_PUBLIC_NODEOPS_REDIRECT_URI ENV NEXT_PUBLIC_NODEOPS_SCOPES=$NEXT_PUBLIC_NODEOPS_SCOPES # Server-side vars — only needed at runtime, read per-request by API routes ENV NODEOPS_CLIENT_SECRET="" ENV NODEOPS_TOKEN_URL=https://id.nodeops.network/oauth2/token ENV NODEOPS_USERINFO_URL=https://autogen-v2-api.nodeops.network/v1/users/me RUN npm run build EXPOSE 3000 CMD ["npm", "start"]Also ensure
.dockerignoreexcludes.env.local:.env .env.local node_modules .nextIf the project deploys to Vercel or similar: no Dockerfile is needed — those platforms inject env vars before building automatically. Just remind the user to add the vars in their platform's dashboard.
-
CreateOS / build-then-inject platforms — CreateOS uses the Dockerfile when one is present, but injects env vars after the build step. This means
NEXT_PUBLIC_*vars areundefinedat build time even with ARG/ENV in the Dockerfile. Use the runtime config workaround below, or remove the Dockerfile entirely to fall back to CreateOS's default install/run commands.What the package handles automatically (v1.x+)
AuthProviderfrom@nodeops-createos/integration-oauthv1.x+ auto-fetches/api/configat runtime ifNEXT_PUBLIC_*vars are missing from the bundle. No extra setup is needed — just make sure the API route exists (step 10a below).If using an older package version, add manually:
10a. Create
app/api/config/route.ts(or.js):import { NextResponse } from 'next/server'; export const dynamic = 'force-dynamic'; export async function GET() { return NextResponse.json({ NEXT_PUBLIC_NODEOPS_AUTH_URL: process.env.NEXT_PUBLIC_NODEOPS_AUTH_URL || 'https://id.nodeops.network/oauth2/auth', NEXT_PUBLIC_NODEOPS_CLIENT_ID: process.env.NEXT_PUBLIC_NODEOPS_CLIENT_ID || '', NEXT_PUBLIC_NODEOPS_REDIRECT_URI: process.env.NEXT_PUBLIC_NODEOPS_REDIRECT_URI || '', NEXT_PUBLIC_NODEOPS_SCOPES: process.env.NEXT_PUBLIC_NODEOPS_SCOPES || 'offline_access offline openid', }); }This route reads
NEXT_PUBLIC_*vars from the server environment at runtime (where they ARE available) and exposes them to the client via a JSON endpoint.10b. Create
components/EnvBootstrap.tsx(or.jsx):"use client"; import { useEffect, useState } from "react"; export default function EnvBootstrap({ children }: { children: React.ReactNode }) { const [ready, setReady] = useState( // If vars are already baked in (e.g. local dev), skip the fetch typeof window !== "undefined" && !!process.env.NEXT_PUBLIC_NODEOPS_CLIENT_ID ); useEffect(() => { if (ready) return; fetch("/api/config") .then((r) => r.json()) .then((env) => { // Patch process.env so AuthProvider picks them up Object.assign(process.env, env); setReady(true); }) .catch((err) => { console.error("Failed to load runtime config:", err); setReady(true); // render anyway so error states show }); }, [ready]); if (!ready) return null; return <>{children}</>; }10c. Update the root layout to wrap
AuthProviderwithEnvBootstrap:import { AuthProvider } from '@nodeops-createos/integration-oauth'; import EnvBootstrap from '@/components/EnvBootstrap'; // inside <body>: <EnvBootstrap> <AuthProvider>{children}</AuthProvider> </EnvBootstrap>This ensures env vars are fetched from the server and patched into
process.envbeforeAuthProviderinitializes on the client. -
Show a summary — print a clear summary:
- Files created/modified (list each one)
- Files skipped (if any were already present)
- Remind user to fill in the 2 required env vars:
NEXT_PUBLIC_NODEOPS_CLIENT_IDandNODEOPS_CLIENT_SECRET - If a Dockerfile exists, remind that
NEXT_PUBLIC_*vars must be set beforenpm run buildin the Dockerfile - Show how to use the hooks in any page:
import { useAuth, useUser } from '@nodeops-createos/integration-oauth'; const { isAuthenticated, login, logout, loading, authError } = useAuth(); const { user } = useUser();