Set up Puck Auth
Puck Auth is experimental and may change in a future release.
Let users sign in to your editor with their Puck Cloud account.
Via the CLI
Set up Puck Auth with the CLI:
npx @puckeditor/core add authManual setup
Serve the sign-in routes
Set auth.provider to "puck". puckHandler will handle authentication redirects, and refuse API access to anyone unauthenticated.
import { puckHandler } from "@puckeditor/cloud-client";
const handleRequest = (request) => {
return puckHandler(request, {
auth: { provider: "puck" },
});
};
export const DELETE = handleRequest;
export const GET = handleRequest;
export const POST = handleRequest;Protect the editor
Protect your pages by checking whether your user has an active session with puck.auth.getSession(), and redirect them to loginUrl() if not.
// app/puck/page.tsx
import puckCloud, { loginUrl } from "@puckeditor/cloud-client";
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { Editor } from "./Editor";
const puck = puckCloud();
export default async function Page() {
const session = await puck.auth.getSession(await headers());
// Come back here after signing in
if (!session) redirect(loginUrl("/puck"));
return <Editor />;
}Install the plugin
The Auth plugin shows the signed-in user in the Puck header. Install the @puckeditor/plugin-auth package:
npm i @puckeditor/plugin-auth --saveLoad the plugin
Create the plugin with createAuthPlugin and add it to <Puck>:
import { createAuthPlugin } from "@puckeditor/plugin-auth";
const authPlugin = createAuthPlugin();
export function Editor() {
return (
<Puck
plugins={[authPlugin]}
// ...
/>
);
}Set the redirect URL
In development, Puck Cloud sends users back to your app on localhost. In production, set your app’s URL, like https://editor.example.com, as the Redirect URL on your project’s Auth page in Puck Cloud.
Invite editors
By default, your organization’s members and admins can sign in to every project. To let someone sign in to your editor, invite them as an Editor from the project’s Auth page.