Skip to Content
Docs
Quickstarts

Quickstart: React SPA

Add Pontive sign-in to a React single-page app built with Vite and React Router, with protected routes and the account menu.

By the end of this page your React app has sign-in and sign-up pages, routes only signed-in users reach, and an account menu. It follows Pontive's React demo: @pontive/pontkit-react in a Vite app with React Router.

1. Set up the app in Pontive

In the Pontive dashboard, on a non-production instance of your project:

  1. Create an app for this SPA and copy its app id (app_…) and the instance's auth domain, e.g. acme-x7k2p9qa-dev.auth.us.pontive.dev.
  2. Add your origins to the app's allowed origins: http://localhost:5173 for the Vite dev server, and each deployed URL.
  3. If you use the account menu, add offline_access, pontive:me:read and pontive:me:manage to the app's allowed scopes and turn refresh tokens on.

Draft

The dashboard labels in this step are written from the API's field names and have not been checked against the current dashboard.

2. Install the SDK

npm install @pontive/pontkit-react
npm install --save-dev @pontive/pontkit-loader

@pontive/pontkit-react has the provider, the hooks and the components, and brings the browser OIDC client (@pontive/oidc-client) with it. @pontive/pontkit-loader is only needed for the Vite dev-server proxy below.

3. Serve the auth endpoints from your origin

A browser treats a cookie as first-party only when it comes from the origin the page is on, and Safari blocks third-party cookies outright. So the SPA calls the auth endpoints on its own origin, under /__pontive, and something forwards them to the auth domain. In development that is Vite's proxy:

vite.config.ts
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import { viteAuthProxy } from "@pontive/pontkit-loader/proxy";

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "");

  return {
    plugins: [react()],
    server: { proxy: viteAuthProxy({ authHost: env.PONTIVE_AUTH_HOST }) },
  };
});
.env.local
# Read by vite.config.ts only. Not VITE_-prefixed, so it is never bundled.
PONTIVE_AUTH_HOST=acme-x7k2p9qa-dev.auth.us.pontive.dev

VITE_PONTIVE_AUTH_DOMAIN=/__pontive
VITE_PONTIVE_APP_ID=app_...
VITE_PONTIVE_API_BASE_URL=https://api.us.pontive.com

In production, add the same forwarding where the built app is hosted: Vercel, Netlify, Cloudflare, CloudFront, nginx and Caddy recipes are in the auth proxy. A host that only serves files (GitHub Pages, a bare S3 website) cannot forward; use a custom auth domain on your own site instead and set VITE_PONTIVE_AUTH_DOMAIN to it.

4. The provider and the routes

src/App.tsx
import { BrowserRouter, Route, Routes, useNavigate } from "react-router-dom";
import { AccountMenu, Authenticated, Provider, SignedIn, SignedOut, Signin, Signup } from "@pontive/pontkit-react";

/**
 * `scope` replaces the default `openid email profile`, so those stay listed.
 * The other three let the account menu read and change the signed-in user;
 * the app must allow them and have refresh tokens on.
 */
const SCOPE = "openid email profile offline_access pontive:me:read pontive:me:manage";

/** A route only a signed-in user may see; others go to the sign-in page through the router. */
function Guarded({ children }: { children: React.ReactNode }) {
  const navigate = useNavigate();
  return <Authenticated onRedirect={(url) => navigate(url)}>{children}</Authenticated>;
}

function Header() {
  return (
    <header>
      <SignedOut>
        <a href="/signin">Sign in</a>
      </SignedOut>
      <SignedIn>
        <AccountMenu />
      </SignedIn>
    </header>
  );
}

export default function App() {
  return (
    <Provider
      domain={import.meta.env.VITE_PONTIVE_AUTH_DOMAIN}
      appId={import.meta.env.VITE_PONTIVE_APP_ID}
      apiBaseUrl={import.meta.env.VITE_PONTIVE_API_BASE_URL}
      signinUrl="/signin"
      signupUrl="/signup"
      signinRedirectUrl="/"
      scope={SCOPE}
    >
      <BrowserRouter>
        <Header />
        <Routes>
          <Route path="/" element={<p>Home</p>} />
          <Route path="/signin/*" element={<Signin />} />
          <Route path="/signup/*" element={<Signup />} />
          <Route
            path="/dashboard"
            element={
              <Guarded>
                <p>Only signed-in users see this.</p>
              </Guarded>
            }
          />
        </Routes>
      </BrowserRouter>
    </Provider>
  );
}
  • <Signin> and <Signup> render the flows configured on the instance, with its branding. Their routes end in /* because a flow can move through sub-paths.
  • <Authenticated> waits for the session to be restored, then renders its children for a signed-in user and sends anyone else to signinUrl. By default that is a full page load; onRedirect hands it to React Router instead. If the app runs under a base path, strip it from url before calling navigate, which adds it back.
  • <SignedIn> and <SignedOut> render nothing until the session is known, so there is no flash of the wrong state.

5. Run it

npm run dev

Open http://localhost:5173/signup, create an account, and you land on / signed in.

Using the session in code

import { useAuthState, useSession } from "@pontive/pontkit-react";

function Status() {
  const { isAuthenticated, resolved } = useAuthState(); // nothing is known until resolved
  const change = useSession(); // the last change: { reason, isAuthenticated, organizationId } or null
  if (!resolved) return null;
  return <p>{isAuthenticated ? `Signed in${change?.organizationId ? ` to ${change.organizationId}` : ""}` : "Signed out"}</p>;
}

The account menu signs the user out. For calls the components do not make (signOut(), getOrganization(), switchOrganization()), import Auth from @pontive/oidc-client: it is the singleton the provider configured, as long as your app resolves the same copy @pontive/pontkit-react depends on. Never call Auth.configure() yourself; pass your own callbacks to the provider's config prop. The client's methods are in its README on npm.

Draft

Calling your own API from the SPA with a token issued for it (resource) is not shown here yet: the browser SDK's documented token methods are for Pontive's own APIs. See Protect an API for the API side.

Next steps