Skip to content

Commit

Permalink
update example with batchCheck call
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyjames committed Dec 17, 2024
1 parent 7f61468 commit c9c1fb1
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions example/example1/example1.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CredentialsMethod, FgaApiValidationError, OpenFgaClient, TypeName } from "@openfga/sdk";
import { randomUUID } from "crypto";

async function main () {
let credentials;
Expand Down Expand Up @@ -137,6 +138,11 @@ async function main () {
Type: "document"
}
}
},
{
user: "user:bob",
relation: "writer",
object: "document:7772ab2a-d83f-756d-9397-c5ed9f3cb69a"
}
]
}, { authorizationModelId });
Expand Down Expand Up @@ -180,6 +186,35 @@ async function main () {
});
console.log(`Allowed: ${allowed}`);

// execute a batch check
const anneCorrelationId = randomUUID();
const { responses } = await fgaClient.batchCheck({
checks: [
{
// should have access
user: "user:anne",
relation: "viewer",
object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
context: {
ViewCount: 100
},
correlationId: anneCorrelationId,
},
{
// should NOT have access
user: "user:anne",
relation: "viewer",
object: "document:7772ab2a-d83f-756d-9397-c5ed9f3cb69a",
}
]
});

const anneAllowed = responses.filter(r => r.correlationId === anneCorrelationId);
console.log(`Anne is allowed access to ${anneAllowed.length} documents`);
anneAllowed.forEach(item => {
console.log(`Anne is allowed access to ${item.request.object}`);
});

console.log("Writing Assertions");
await fgaClient.writeAssertions([
{
Expand Down

0 comments on commit c9c1fb1

Please sign in to comment.