Auth with React (Vite)
In a single-page app, such as a React app built with Vite, the browser talks to Exwave Auth directly and keeps the session itself. This page explains the provider, how the session behaves, and how to protect a page.
If you made your app with npm create exwave --template vite, or added
@exwave/auth-react with the shadcn CLI, most of this is set up already. See
Get started to set it up.
The provider
Section titled “The provider”Install the packages:
npm install @exwave-nexus/auth @exwave-nexus/auth-reactThen wrap your app in <ExwaveProvider>. Every screen and hook needs it. A
full example with React Router is in
Get started, step 4.
import { ExwaveProvider } from "@exwave-nexus/auth-react";
<ExwaveProvider publishableKey={import.meta.env.VITE_EXWAVE_PUBLISHABLE_KEY} navigate={(to, options) => router.navigate(to, options)}> <App /></ExwaveProvider>| Prop | Default | What it does |
|---|---|---|
publishableKey |
Required | Your publishable key, from Nexus → Auth. |
navigate |
A full page load | How the screens change pages. Pass your router’s navigate function. |
paths |
See Components | Where your auth pages are. |
messages |
English | Your own text for messages, by key. See Change the words. |
storage |
"local" |
Where the browser keeps the session: "local" (localStorage), "session" (sessionStorage), "memory", or your own storage. |
onAuthChange |
None | A function called when someone signs in or out, in this tab or another one. |
How the session behaves
Section titled “How the session behaves”With the default "local" storage, people stay signed in when they reload the
page. All tabs share the session: when someone signs in or out in one tab, the
others update too. While the page is open, the session renews itself in the
background.
When someone is signed out, signOutReason says why, for five minutes. So if
they reload the sign-in page, it can still say “You signed out on every
device” or “This account was locked”.
Protect a page
Section titled “Protect a page”This page sends people who aren’t signed in to the sign-in page:
import { useExwave } from "@exwave-nexus/auth-react";import { useEffect } from "react";
export function AccountPage() { const { isLoaded, user, navigate } = useExwave(); useEffect(() => { if (isLoaded && !user) navigate("/sign-in?redirect=/account", { replace: true }); }, [isLoaded, user, navigate]); if (!user) return null; return <h1>{user.displayName}</h1>;}This check runs in the browser, so it only decides what the page shows. It doesn’t keep data safe. If your own server needs to know who is signed in, use Next.js, which keeps the session on the server.
Without React
Section titled “Without React”@exwave-nexus/auth is the client that the React and Next.js packages are
built on. It doesn’t use any framework, so you can use it with any framework,
or none:
import { createExwaveAuth } from "@exwave-nexus/auth";
const auth = createExwaveAuth({ publishableKey: "exw_pk_..." });
// Called once when the session has loaded, then each time it changes.auth.onAuthStateChange((event, session) => { console.log(event, session?.user.displayName ?? "signed out");});