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:
- 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. - Add your origins to the app's allowed origins:
http://localhost:5173for the Vite dev server, and each deployed URL. - If you use the account menu, add
offline_access,pontive:me:readandpontive:me:manageto 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:
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 }) },
};
});# 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.comIn 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
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 tosigninUrl. By default that is a full page load;onRedirecthands it to React Router instead. If the app runs under a base path, strip it fromurlbefore callingnavigate, 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 devOpen 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
- The framework bindings: every component, prop and event.
- Organizations and roles.
Quickstart: Next.js
Add Pontive sign-in to a Next.js App Router app, with the auth endpoints on your own origin, the session readable on the server and the account menu in the header.
Unclaimed instances
Start building with no Pontive account. An unclaimed development instance works at once, is deleted after 7 days, and moves intact into your workspace when you claim it.