+
Impersonate a user
+
All requests will use the selected user's identity and permissions until you return to your account.
+
+
+
+
+ {error &&
{error}
}
+
+
+ );
+}
diff --git a/src/lib/impersonation.ts b/src/lib/impersonation.ts
new file mode 100644
index 0000000..1ab6071
--- /dev/null
+++ b/src/lib/impersonation.ts
@@ -0,0 +1,46 @@
+import type { Payload } from "payload";
+
+export const IMPERSONATION_COOKIE = "ptf-impersonation-original";
+export const IMPERSONATION_ACTIVE_COOKIE = "ptf-impersonation-active";
+
+export type ImpersonationUser = {
+ id: number | string;
+ username?: string | null;
+ displayName?: string | null;
+};
+
+export function getImpersonationCookieOptions(maxAge: number) {
+ return {
+ httpOnly: true,
+ sameSite: "lax" as const,
+ secure: process.env.NODE_ENV === "production",
+ path: "/",
+ maxAge,
+ };
+}
+
+export function userLabel(user: ImpersonationUser): string {
+ return user.displayName || user.username || `User ${user.id}`;
+}
+
+export function isImpersonationTarget(
+ operator: ImpersonationUser,
+ target: ImpersonationUser,
+): boolean {
+ return String(operator.id) !== String(target.id);
+}
+
+export async function findImpersonationUsers(payload: Payload) {
+ return payload.find({
+ collection: "users",
+ depth: 0,
+ limit: 1000,
+ pagination: false,
+ sort: "username",
+ select: {
+ username: true,
+ displayName: true,
+ },
+ overrideAccess: true,
+ });
+}