Auth Context
AuthContext provides authentication state and user permissions to all CRUD components. Used to conditionally show/hide actions and fields based on user roles.
Import
import { AuthContext } from "@phpcreation/frontend-crud-react-nextjs-bundle/contexts/AuthContext";What It Provides
| Value | Description |
|---|---|
user | Current authenticated user object |
permissions | User’s permission set |
hasPermission | Helper to check a specific permission |
isAuthenticated | Boolean auth state |
Usage
import { AuthContext } from "...";
// In your app root or layout
<AuthContext user={currentUser} permissions={userPermissions}>
<CRUDPage />
</AuthContext>useAuthContext Hook
import { useAuthContext } from "@phpcreation/frontend-crud-react-nextjs-bundle/contexts/AuthContext";
function ActionButton() {
const { hasPermission } = useAuthContext();
if (!hasPermission("entity:delete")) return null;
return <DeleteButton />;
}verifyUser Utility
Use verifyUser for server-side authentication checks in Next.js route handlers or page server components:
import { verifyUser } from "@phpcreation/frontend-crud-react-nextjs-bundle/utils/verifyUser";
export default async function Page() {
const user = await verifyUser(); // redirects to login if unauthenticated
return <EntityPage user={user} />;
}AuthContext integrates with the bundle’s action button system — actions with insufficient permissions are automatically hidden.
Related
Last updated on