API Platform Admin delegates the authentication support to React Admin. Refer to the chapter dedicated to authentication in the React Admin documentation for more information.
In short, you have to tweak the data provider and the API documentation parser like this:
// pwa/pages/admin/index.tsx
import Head from "next/head";
import { Redirect, Route } from "react-router-dom";
import {
fetchHydra as baseFetchHydra,
hydraDataProvider as baseHydraDataProvider,
useIntrospection,
} from "@api-platform/admin";
import { parseHydraDocumentation } from "@api-platform/api-doc-parser";
import authProvider from "utils/authProvider";
import { ENTRYPOINT } from "config/entrypoint";
const getHeaders = () => localStorage.getItem("token") ? {
Authorization: `Bearer ${localStorage.getItem("token")}`,
} : {};
const fetchHydra = (url, options = {}) =>
baseFetchHydra(url, {
...options,
headers: getHeaders,
});
const RedirectToLogin = () => {
const introspect = useIntrospection();
if (localStorage.getItem("token")) {
introspect();
return <></>;
}
return <Redirect to="/login" />;
};
const apiDocumentationParser = async () => {
try {
return await parseHydraDocumentation(ENTRYPOINT, { headers: getHeaders });
} catch (result) {
const { api, response, status } = result;
if (status !== 401 || !response) {
throw result;
}
// Prevent infinite loop if the token is expired
localStorage.removeItem("token");
return {
api,
response,
status,
customRoutes: [
<Route key="/" path="/" component={RedirectToLogin} />
],
};
}
};
const dataProvider = baseHydraDataProvider({
entrypoint: ENTRYPOINT,
httpClient: fetchHydra,
apiDocumentationParser,
});
const AdminLoader = () => {
if (typeof window !== "undefined") {
const { HydraAdmin } = require("@api-platform/admin");
return <HydraAdmin dataProvider={dataProvider} authProvider={authProvider} entrypoint={window.origin} />;
}
return <></>;
};
const Admin = () => (
<>
<Head>
<title>API Platform Admin</title>
</Head>
<AdminLoader />
</>
);
export default Admin;
For the implementation of the auth provider, you can find a working example in the API Platform’s demo application.
Made with love by
Les-Tilleuls.coop can help you design and develop your APIs and web projects, and train your teams in API Platform, Symfony, Next.js, Kubernetes and a wide range of other technologies.
Learn more