Start the flow
#A route handler creates state and the verifier, stores them in a short-lived httpOnly cookie scoped to the auth path, and redirects to OzVault. Set OZVAULT_CLIENT_ID, OZVAULT_CLIENT_SECRET and OZVAULT_REDIRECT_URI as server environment variables; never prefix them with NEXT_PUBLIC_.
// app/api/auth/ozvault/route.ts — starts the flow
import { NextResponse } from "next/server";
import { randomBytes, createHash } from "node:crypto";
export async function GET() {
const state = randomBytes(16).toString("base64url");
const verifier = randomBytes(32).toString("base64url");
const url = new URL("https://ozvault.lol/oauth/authorize");
url.search = new URLSearchParams({
response_type: "code",
client_id: process.env.OZVAULT_CLIENT_ID!,
redirect_uri: process.env.OZVAULT_REDIRECT_URI!, // e.g. https://example.com/api/auth/ozvault/callback
scope: "openid profile:read storage:app:read storage:app:write offline_access",
state,
code_challenge: createHash("sha256").update(verifier).digest("base64url"),
code_challenge_method: "S256",
}).toString();
const res = NextResponse.redirect(url);
res.cookies.set("ozv_oauth", state + "." + verifier, {
httpOnly: true, secure: true, sameSite: "lax", path: "/api/auth/ozvault", maxAge: 600,
});
return res;
}Handle the callback on the server
#The callback verifies state and iss, exchanges the code and stores tokens server-side (saveConnection is your database code).
// app/api/auth/ozvault/callback/route.ts — exchanges the code on the server
import { NextRequest, NextResponse } from "next/server";
import { saveConnection } from "@/lib/ozvault-store"; // your database
export async function GET(req: NextRequest) {
const [state, verifier] = (req.cookies.get("ozv_oauth")?.value ?? "").split(".");
const params = req.nextUrl.searchParams;
if (!state || params.get("state") !== state) return new NextResponse("Invalid state", { status: 400 });
if (params.get("iss") !== "https://ozvault.lol") return new NextResponse("Unexpected issuer", { status: 400 });
if (params.get("error")) return NextResponse.redirect(new URL("/?ozvault=declined", req.url));
const r = await fetch("https://ozvault.lol/oauth/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: "Basic " + Buffer.from(process.env.OZVAULT_CLIENT_ID + ":" + process.env.OZVAULT_CLIENT_SECRET).toString("base64"),
},
body: new URLSearchParams({
grant_type: "authorization_code",
code: params.get("code") ?? "",
redirect_uri: process.env.OZVAULT_REDIRECT_URI!,
code_verifier: verifier,
}),
cache: "no-store",
});
const tokens = await r.json();
if (!r.ok) return new NextResponse(tokens.error_description, { status: 502 });
await saveConnection(tokens); // store server-side; never in localStorage
const res = NextResponse.redirect(new URL("/dashboard", req.url));
res.cookies.delete({ name: "ozv_oauth", path: "/api/auth/ozvault" });
return res;
}Call the API from server code
#Use tokens only in route handlers, server actions or server components.
// app/actions.ts — server-only use of the access token
"use server";
import { getAccessToken } from "@/lib/ozvault-store"; // refreshes when needed (see the token guide)
export async function saveDraft(text: string) {
const token = await getAccessToken();
const r = await fetch("https://api.ozvault.lol/v1/app/files?" + new URLSearchParams({ name: "drafts/" + Date.now() + ".md", contentType: "text/markdown" }), {
method: "POST",
headers: { Authorization: "Bearer " + token, "Content-Type": "application/octet-stream" },
body: text,
cache: "no-store",
});
if (!r.ok) throw new Error((await r.json()).error.message);
return r.json();
}Clear documentation is part of the product.