React with Vite
Add sign-in to a React single-page app built with Vite and deployed on Ploy.
React with Vite
This guide adds Ploy Auth to a React app built with Vite: drop-in forms in the browser and a worker route that knows who is signed in. The complete code is in examples/vite-react-auth.
1. Configure the project
kind: dynamic
build: pnpm build
out: dist
assets:
binding: ASSETS
not_found_handling: single-page-application
run_worker_first:
- /api/*
- /_ploy/*
auth:
binding: AUTH_DBrun_worker_first sends /_ploy/* past the single-page fallback, so the auth endpoints and hosted pages are reachable during local development.
pnpm add @meetploy/auth-react
pnpm add -D @meetploy/cli @meetploy/viteimport { ploy } from "@meetploy/vite";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [react(), ...ploy()],
});2. Wrap the app
import { AuthProvider } from "@meetploy/auth-react";
import { createRoot } from "react-dom/client";
import App from "./App";
createRoot(document.getElementById("root")!).render(
<AuthProvider>
<App />
</AuthProvider>,
);3. Show sign-in or the account
import {
SignedIn,
SignedOut,
SignInForm,
UserButton,
UserProfile,
} from "@meetploy/auth-react";
export default function App() {
return (
<main>
<header>
<SignedIn>
<UserButton afterSignOutUrl="/" />
</SignedIn>
</header>
<SignedOut>
<SignInForm />
</SignedOut>
<SignedIn>
<UserProfile />
</SignedIn>
</main>
);
}To host auth on a sibling subdomain, configure shared sessions first and pass apiUrl="https://auth.example.com/_ploy/auth" to AuthProvider.
The forms show whatever sign-in methods are enabled for the project. See React components for every component, prop, and theming option.
4. Protect a worker route
The browser sends the ploy_session cookie with same-origin requests. Verify it in the worker with env.PLOY_AUTH:
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname === "/api/profile") {
const token = request.headers
.get("Cookie")
?.match(/(?:^|;\s*)ploy_session=([^;]+)/)?.[1];
const user = token ? await env.PLOY_AUTH.getUser(token) : null;
if (!user) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}
return Response.json({ userId: user.id });
}
return new Response("Not found", { status: 404 });
},
} satisfies Ploy;From React, call it with a plain fetch("/api/profile"). Run ploy types to add PLOY_AUTH to your generated env.d.ts.
The binding extends due sessions in the database, but cannot renew browser cookies. AuthProvider checks /_ploy/auth/me on mount, not on a timer. For a long-lived single-page view, make another same-origin /me request before the idle lifetime ends to renew its cookie. See Sessions for refresh intervals, limits, and revocation behavior.
To call Google or GitHub APIs for the user, configure additional scopes and call env.PLOY_AUTH.getProviderToken(sessionToken, provider) inside the worker. Provider tokens are not exposed by useAuth(); return only the API data your UI needs.
Server-side lifecycle hooks
Add an auth property alongside fetch on your worker's default export; see auth hooks. If you use Ploy Start, register .auth({ ... }) before .build() (example). Hooks run on the server, not in AuthProvider or useAuth(). Deployed hooks require dashboard opt-in.
5. Run it locally
pnpm ploy devploy dev serves the auth endpoints and hosted pages on your app's own origin and provides env.PLOY_AUTH, backed by a local database. Password sign-in and passkeys work on localhost; add PLOY_AUTH_* credentials to .env to try social sign-in.
Want to skip the forms entirely? Link to /_ploy/auth/sign-in?redirectTo=/
and use the hosted pages instead.
How is this guide?
Last updated on