System Information
- Strapi Version: 5.15.0/5.21.0
- Operating System:
- Database:
- Node Version: v21.5.0
- NPM Version: 10.2.4
- Yarn Version:
Problem:
I’m using the new useAuth hook from @strapi/strapi/admin in Strapi v5. It works perfectly in develop mode, but when I run npm run build && npm run start, the hook returns undefined, but only when used inside a custom plugin.
Works (custom component outside plugin):
// ./src/admin/app.tsx
register(app) {
app.addMenuLink({
to: "testing/dwa",
icon: Calendar,
intlLabel: {
id: "test.plugin.menuItems.test",
defaultMessage: "test",
},
Component: async () => {
const { Test } = await import("./Test");
return Test;
},
permissions: [],
});
}
// ./src/admin/Test.tsx
import { useAuth } from "@strapi/strapi/admin";
export const Test = () => {
const data = useAuth("Test", (data) => data);
return <pre>{JSON.stringify(data, null, 2)}</pre>;
};
→ Works in both develop and start.
Doesn’t work (inside plugin created with npx @strapi/sdk-plugin init):
// ./src/plugins/my-plugin/admin/index.ts
register(app) {
app.addMenuLink({
to: `plugins/my-plugin`,
icon: PluginIcon,
intlLabel: {
id: "my-plugin.plugin.name",
defaultMessage: "my-plugin",
},
Component: async () => {
const { Test } = await import("./Test");
return Test;
},
});
app.registerPlugin({
id: "my-plugin",
initializer: Initializer,
isReady: false,
name: "my-plugin",
});
}
// ./src/plugins/my-plugin/admin/Test.tsx
import { useAuth } from "@strapi/strapi/admin";
export const Test = () => {
const data = useAuth("Test", (data) => data);
return <pre>{JSON.stringify(data, null, 2)}</pre>;
};
→ Works in develop
→ In build/start: useAuth returns undefined
Question:
Is this expected behavior? Are core admin hooks like useAuth not available inside custom plugins in production builds? If so — what’s the recommended approach?