Skip to content

Latest commit

 

History

History
51 lines (36 loc) · 1.46 KB

authorization.md

File metadata and controls

51 lines (36 loc) · 1.46 KB

Authorization

How do define and use a new permission?

  1. Add permission schema to the PermissionSet.ts file.
  2. Add logic for how to assign this permission to a user in the getGivenPermissionSets.ts file.
  3. Use the permission in the application code:

Example: Can the current user read a resource of type "myResource" with id "1"?

import { authenticate } from "@/authentication/authenticateAndAuthorize";

const authentication = await authenticate();
if (
  !authentication.authorize("myResource", "read", [{ key: "id", value: "1" }])
)
  throw new Error("Forbidden");

// Do something

Example: Authenticate and authorize a server action

import { authenticateAction } from "@/authentication/authenticateAndAuthorize";

export const createMyResourceAction = async () => {
  const authentication = await authenticateAction("createMyResourceAction");
  authentication.authorizeAction("myResource", "create");

  // Do something
};

Example: Authenticate and authorize a page view

import { authenticatePage } from "@/authentication/authenticateAndAuthorize";

export default async function Page({ params }) {
  const { id } = await params;

  const authentication = await authenticatePage("/admin/myResource/[id]");
  authentication.authorizePage("myResource", "create", [
    { key: "id", value: id },
  ]);

  // Render the page
}