Skip to content

Last1 ID SDK Usage (TypeScript helper)

Reference helper: shared/oidcClient.ts

Build authorize URL with PKCE

import { buildAuthorizeUrl, createPkcePair } from "../shared/oidcClient";

const pkce = createPkcePair();
const authorizeUrl = buildAuthorizeUrl(
  {
    issuerUrl: "https://auth.last1.id",
    clientId: process.env.LAST1_CLIENT_ID!, // Copy Client ID (UUID) or App ID slug
    redirectUri: "https://your-app.com/auth/callback",
    scopes: ["openid", "profile", "offline_access", "identity:read"],
  },
  pkce
);

// Persist pkce.codeVerifier + state + nonce in secure session storage.
window.location.href = authorizeUrl;

Exchange authorization code

const params = new URLSearchParams({
  grant_type: "authorization_code",
  code,
  redirect_uri: "https://your-app.com/auth/callback",
  client_id: process.env.LAST1_CLIENT_ID!,
  code_verifier: pkceVerifierFromSession,
});

const tokenRes = await fetch("https://auth.last1.id/oauth/token", {
  method: "POST",
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
  body: params.toString(),
});

Refresh token rotation

const params = new URLSearchParams({
  grant_type: "refresh_token",
  refresh_token,
  client_id: process.env.LAST1_CLIENT_ID!,
});

const tokenRes = await fetch("https://auth.last1.id/oauth/token", {
  method: "POST",
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
  body: params.toString(),
});