Auth Quick Start
Sign in with Lovelace is standard OAuth 2.0 and OpenID Connect, so any
conforming client library works against the Lovelace authorization server. This
guide adds sign-in to a Next.js (App Router) app with
openid-client: a user signs in,
your server exchanges the authorization code for tokens, and your server calls
Lattice Cloud as that user.
The
@lovelace-ai/auth-reactand@lovelace-ai/auth-nextjspackages are not yet published to npm and cannot be installed from the registry. Use a standard OAuth 2.0 or OpenID Connect client library, as this guide does.
Authorization server#
| Item | Value |
|---|---|
| Issuer | https://auth.uselovelace.com |
| OAuth discovery | https://auth.uselovelace.com/.well-known/oauth-authorization-server |
| OpenID discovery | https://auth.uselovelace.com/.well-known/openid-configuration |
| Authorization endpoint | https://auth.uselovelace.com/oauth/authorize |
| Token endpoint | https://auth.uselovelace.com/oauth/token |
| PKCE method | S256 |
| Client authentication | client_secret_basic, client_secret_post, or none (public client) |
Before you start#
Register an OAuth app in the developer portal:
- Set the redirect URI to your callback route. For local development, use
http://localhost:3000/auth/callback. - Select the
openidscope, pluslattice:compute:inferenceif your server calls Lattice Cloud as the user. - Leave Confidential app checked so the portal issues a client secret. Copy the client ID and the client secret.
From a terminal,
ada developer app create --redirect-uri http://localhost:3000/auth/callback
does the same: it registers a confidential app whose default scopes include
openid and lattice:compute:inference, and writes LOVELACE_CLIENT_ID and
LOVELACE_CLIENT_SECRET to .env.local (or .env when there is no
.env.local) in the project root.
Local development needs no approval step. An app whose redirect URIs all use
localhost, a 127.x.x.x address, or [::1] (any port and path) works as
soon as you create it, and creating your first app requests your developer
account. A production redirect URI needs that account approved; see
Going live.
Install the OAuth client and the OpenAI SDK (Lattice Cloud serves an OpenAI-compatible API):
pnpm add openid-client openai
1. Configure the client#
# .env.local
LOVELACE_CLIENT_ID=your-client-id
LOVELACE_CLIENT_SECRET=your-client-secret
LOVELACE_REDIRECT_URI=http://localhost:3000/auth/callback
Discover the authorization server once and reuse the configuration:
// lib/lovelace-oauth.ts
import * as client from "openid-client";
let config: Promise<client.Configuration> | undefined;
export function getOAuthConfig(): Promise<client.Configuration> {
config ??= client.discovery(
new URL("https://auth.uselovelace.com"),
process.env.LOVELACE_CLIENT_ID!,
undefined,
client.ClientSecretBasic(process.env.LOVELACE_CLIENT_SECRET!),
);
return config;
}
2. Add the sign-in route#
The sign-in route creates a PKCE verifier and a state value, keeps them in short-lived cookies, and redirects the user to the authorization endpoint:
// app/auth/login/route.ts
import { NextResponse } from "next/server";
import * as client from "openid-client";
import { getOAuthConfig } from "@/lib/lovelace-oauth";
const flowCookie = {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax" as const,
path: "/",
maxAge: 600,
};
export async function GET() {
const config = await getOAuthConfig();
const codeVerifier = client.randomPKCECodeVerifier();
const state = client.randomState();
const authorizationUrl = client.buildAuthorizationUrl(config, {
redirect_uri: process.env.LOVELACE_REDIRECT_URI!,
scope: "openid lattice:compute:inference",
code_challenge: await client.calculatePKCECodeChallenge(codeVerifier),
code_challenge_method: "S256",
state,
});
const response = NextResponse.redirect(authorizationUrl);
response.cookies.set("lovelace_pkce_verifier", codeVerifier, flowCookie);
response.cookies.set("lovelace_oauth_state", state, flowCookie);
return response;
}
Link to it from anywhere in your UI:
<a href="/auth/login">Sign in with Lovelace</a>
3. Add the callback route#
The callback route checks the state, exchanges the code and the PKCE verifier for tokens, and stores the access token in an HTTP-only cookie:
// app/auth/callback/route.ts
import { NextResponse, type NextRequest } from "next/server";
import * as client from "openid-client";
import { getOAuthConfig } from "@/lib/lovelace-oauth";
export async function GET(request: NextRequest) {
const codeVerifier = request.cookies.get("lovelace_pkce_verifier")?.value;
const state = request.cookies.get("lovelace_oauth_state")?.value;
if (codeVerifier == null || state == null) {
return NextResponse.redirect(new URL("/", request.url));
}
const config = await getOAuthConfig();
const tokens = await client.authorizationCodeGrant(
config,
new URL(request.url),
{ pkceCodeVerifier: codeVerifier, expectedState: state },
);
const response = NextResponse.redirect(new URL("/dashboard", request.url));
response.cookies.delete("lovelace_pkce_verifier");
response.cookies.delete("lovelace_oauth_state");
response.cookies.set("lovelace_access_token", tokens.access_token, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
path: "/",
maxAge: tokens.expires_in,
});
return response;
}
tokens.claims() returns the verified ID token claims, including the user's
sub. For production, keep tokens in a server-side session store and put only
a session ID in the cookie.
4. Call Lattice Cloud as the user#
In a Server Component or route handler, read the access token and send it as the bearer token to Lattice Cloud. The request runs on the signed-in user's compute:
// app/dashboard/page.tsx
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import OpenAI from "openai";
export default async function Dashboard() {
const accessToken = (await cookies()).get("lovelace_access_token")?.value;
if (accessToken == null) {
redirect("/auth/login");
}
const lattice = new OpenAI({
apiKey: accessToken,
baseURL: "https://lattice.uselovelace.com/v1",
});
const completion = await lattice.chat.completions.create({
model: "lovelace:llama-3.2-3b-instruct",
messages: [{ role: "user", content: "Say hello." }],
});
return <p>{completion.choices[0]?.message.content}</p>;
}
To keep users signed in past the access token's lifetime, add the
offline_access scope to the app and to the authorization request, store
tokens.refresh_token, and exchange it with client.refreshTokenGrant.
How the flow works#
/auth/loginredirects the user tohttps://auth.uselovelace.com/oauth/authorizewith a PKCE challenge and a state value.- The user signs in to Lovelace and approves the scopes your app requested.
- Lovelace redirects to
/auth/callbackwith an authorization code.authorizationCodeGrantchecks the state and exchanges the code and the verifier athttps://auth.uselovelace.com/oauth/token. - Your server sends the access token as a bearer token to Lattice Cloud.
Next#
- OpenAI-compatible API: endpoints and model names for Lattice Cloud
- External integration: third-party relying-party details
- Production hardening: before you ship
- Troubleshooting