From aac233aabfc764e037d4d3d5b68e6a0909075516 Mon Sep 17 00:00:00 2001 From: Mateusz Wiktor Date: Fri, 23 Oct 2026 11:32:42 +0200 Subject: [PATCH] docs: add Access Manager categories snippets Add grantTokenWithCategories, parseTokenWithCategories and grantWithCategories docs snippets. Co-Authored-By: Claude Sonnet 5.5 --- docs-snippets/access-manager.ts | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/docs-snippets/access-manager.ts b/docs-snippets/access-manager.ts index fefa08e24..4c62519a7 100644 --- a/docs-snippets/access-manager.ts +++ b/docs-snippets/access-manager.ts @@ -231,3 +231,53 @@ try { ); } // snippet.end + +// snippet.grantTokenWithCategories +// Grants `my-authorized-uuid` permission to list all channel metadata and all uuid metadata. +// Category permissions support only the `get` operation. +try { + const token = await pubnub.grantToken({ + ttl: 15, + authorizedUserId: 'my-authorized-uuid', + categories: { + channels: { get: true }, + uuids: { get: true }, + }, + }); + console.log('Granted token:', token); +} catch (error) { + console.error( + `Grant token error: ${error}.${ + (error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : '' + }`, + ); +} +// snippet.end + +// snippet.parseTokenWithCategories +// Decodes a token that was granted with category permissions. +const parsedToken = pubnub.parseToken('use-token-string-generated-by-grantToken()'); + +console.log('Can list all channel metadata:', parsedToken?.categories?.channels?.get); +console.log('Can list all uuid metadata:', parsedToken?.categories?.uuids?.get); +// snippet.end + +// snippet.grantWithCategories +// Grants the auth key permission to list all channel metadata and all uuid metadata. +// This legacy `grant` method requires the secret key to be set on the client. +try { + const response = await pubnub.grant({ + authKeys: ['my-auth-key'], + categories: ['channels', 'uuids'], + get: true, + ttl: 15, + }); + console.log('Grant response:', response); +} catch (error) { + console.error( + `Grant error: ${error}.${ + (error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : '' + }`, + ); +} +// snippet.end