test-cerbos-nestjs
09/19/2024, 7:33 AMimport { useCerbos } from '@cerbos/react';
const cerbos = useCerbos();
const check = await cerbos.checkResource({
resource: {
kind: 'test',
id: testID ?? '',
},
actions: ['view'],
});
console.log('check',JSON.stringify(check))
const isAuthorized = check.isAllowed('view');
oguzhan
test-cerbos-nestjs
09/19/2024, 8:25 AMexport const getCerbosClient = () => {
const client = new Cerbos(
new AutoUpdatingLoader(
'...cerbos embeded PDP url'
)
);
return client;
};
const client = getCerbosClient();
<CerbosProvider
client={client}
principal={{
id: currentUser?.id ?? '',
roles: ['owner'],
}}
>
const cerbos = useCerbos();
fyi I can read principle properties from this code above 🙂
and I used this repo as reference.test-cerbos-nestjs
09/19/2024, 8:40 AMtest-cerbos-nestjs
09/19/2024, 8:44 AMHasan Ayan
09/19/2024, 8:46 AMoguzhan
test-cerbos-nestjs
09/19/2024, 8:47 AM"@cerbos/embedded": "^0.7.3",
"@cerbos/grpc": "^0.18.1",
"@cerbos/react": "^0.1.3",
these are the three cerbos packages i installedAndrew Haines (Cerbos)
@cerbos/core
because it is a dependency of the other packages.
Could you share a more complete example please? Your initial snippet seems like it is executing outside a component but that would mean the useCerbos
hook wouldn't work.
I would suggest trying one of the higher-level hooks (e.g. useIsAllowed
) because then you don't have to deal with promises. The readme has an example.test-cerbos-nestjs
09/19/2024, 9:36 AMconst check = useCheckResource({
resource: {
kind: 'project1',
id: projectID ?? '',
},
actions: ['read']
});
console.log('check',check);
and it always returns isLoading: true
Andrew Haines (Cerbos)
test-cerbos-nestjs
09/19/2024, 9:47 AMuseCerbos
hook, if I am able to retrieve the principal property from it, that means that hook works right?
const cerbos = useCerbos();
console.log(cerbos.principal)
test-cerbos-nestjs
09/19/2024, 10:04 AMconst checkAccess = useCallback(async () => {
console.log('checkAccess...');
try {
console.log(cerbos.principal)
const check = await cerbos.checkResource({
resource: {
kind: 'test',
id: 'testId' ?? '',
},
actions: ['view', 'update', 'delete'],
});
console.log('check', check);
const isAuthorized = check.isAllowed('read');
console.log(isAuthorized, 'isAuthorized');
if (!isAuthorized) {
window.location.href = '/dashboard';
}
} catch (error) {
console.error('Error during course retrieval or authorization check:', error);
}
}, [cerbos]);
useEffect(() => {
checkAccess();
}, [checkAccess]);
this is the function that i used to call checkResource methodtest-cerbos-nestjs
09/19/2024, 10:19 AMtest-cerbos-nestjs
09/19/2024, 10:23 AMtest-cerbos-nestjs
09/19/2024, 10:54 AMtest-cerbos-nestjs
09/19/2024, 10:57 AMtest-cerbos-nestjs
09/19/2024, 11:44 AMcheckResource
function started and never finished..Andrew Haines (Cerbos)
node_modules/@cerbos/embedded/lib/bundle.js
and chuck some console.log
statements into the checkResources
method there. Maybe the download
and instantiateStreaming
functions as well. Hopefully that might narrow down where it is hanging.test-cerbos-nestjs
09/19/2024, 11:56 AMtest-cerbos-nestjs
09/19/2024, 12:18 PMHasan Ayan
09/19/2024, 12:21 PM--force
flag so that vite ignores the build cachetest-cerbos-nestjs
09/19/2024, 12:39 PMAndrew Haines (Cerbos)
instantiate
is hanging?test-cerbos-nestjs
09/19/2024, 12:41 PMtest-cerbos-nestjs
09/19/2024, 12:44 PMinstantiate
Andrew Haines (Cerbos)
test-cerbos-nestjs
09/19/2024, 12:48 PMtest-cerbos-nestjs
09/19/2024, 12:53 PMAndrew Haines (Cerbos)
bundleUrl = "<https://lite.cerbos.cloud/bundle?workspace=P2L30LRNW40G&label=5cd4136b20fda5d37a12aad519a745dcac21758883dc5ca23ab6394f7002c225>"
response = fetch(bundleUrl)
await WebAssembly.instantiateStreaming(response, { env: { now: () => 0 } })
(you can use your own bundle URL or borrow mine 🙂)
I pasted that in the devtools console in both Chrome and Firefox, and it works in both browsers.
Does that script work for you or does it also hang? You'll need to have the console open on a site that doesn't have a content security policy blocking the request (google.com works).test-cerbos-nestjs
09/19/2024, 1:02 PMtest-cerbos-nestjs
09/19/2024, 1:08 PMtest-cerbos-nestjs
09/19/2024, 1:14 PMAndrew Haines (Cerbos)
signal.addEventListener("abort", () => { console.log("aborted") });
to the download
method of AutoUpdatingLoader
in lib/loader.js
to checktest-cerbos-nestjs
09/20/2024, 5:40 AMdownload
method, but no log was printedtest-cerbos-nestjs
09/20/2024, 5:44 AMtest-cerbos-nestjs
09/20/2024, 5:47 AMtest-cerbos-nestjs
09/20/2024, 5:58 AMtest-cerbos-nestjs
09/20/2024, 6:03 AMtest-cerbos-nestjs
09/20/2024, 6:54 AMtest-cerbos-nestjs
09/20/2024, 6:59 AMAndrew Haines (Cerbos)
connect-src <https://lite.cerbos.cloud>
) and executing them (script-src 'wasm-unsafe-eval'
).Andrew Haines (Cerbos)