React Router
npx degit https://github.com/keycloakify/oidc-spa/examples/react-router oidc-spa-react-router
cd oidc-spa-react-router
cp .env.local.sample .env.local
yarn
yarn devLast updated
Was this helpful?
Was this helpful?
import type { Config } from "@react-router/dev/config";
export default {
// Config options...
// Server-side render by default, to enable SPA mode set this to `false`
ssr: false
} satisfies Config;import { useState, useEffect } from "react";
import { withLoginEnforced, fetchWithAuth } from "../oidc.client";
const Invoices = withLoginEnforced(
() => {
const [invoices, setInvoices] = useState<Invoice[] | undefined>(undefined);
useEffect(() => {
fetchWithAuth("/api/invoices")
.then(r => r.json())
.then(setInvoices);
}, []);
if (invoices === undefined) {
return <div>Loading invoices...</div>;
}
return (
<div>
{invoices.map(invoice => (
<div key={invoice.id}>{invoice.amount}</div>
))}
</div>
);
},
{
onRedirecting: () => <div>Redirecting to login...</div>
}
);
export default Invoices;import { enforceLogin, fetchWithAuth } from "../oidc.client";
import type { Route } from "./+types/invoices";
import { useLoaderData } from "react-router";
export async function clientLoader(params: Route.ClientLoaderArgs) {
await enforceLogin(params);
// If we are here, the user is logged in.
const invoices = await fetchWithAuth("/api/invoices").then(r => r.json());
return invoices;
}
export function HydrateFallback() {
return <div>Loading invoices...</div>;
}
export default function Invoices() {
const invoices = useLoaderData<typeof clientLoader>();
return (
<div>
{invoices.map(invoice => (
<div key={invoice.id}>{invoice.amount}</div>
))}
</div>
);
}npx degit https://github.com/keycloakify/oidc-spa/examples/react-router-framework oidc-spa-react-router
cd oidc-spa-react-router
cp .env.local.sample .env.local
yarn
yarn dev