Search.../

add( )

Sets a permissions handler for a specific channel or channel resource.

Description

The handler set by the add() function is used for permission checks by the permissions router for the channel or channel resource specified in the channel property.

Adding a handler to a specific channel without a specified resource adds a handler for all permissions checks on the channel and all permissions checks on any of the channel's resources that do not have their own permissions handlers.

For example, suppose you have the following channel, resources, and permissions:

  • channel: A, permissions: handler X is specified using the add() function
  • channel: A, resource: 1, permissions: handler Y is specified using the add() function
  • channel: A, resource: 2, permissions: no handler is specified

Permissions will be checked as follows:

  • A: use handler X
  • A, 1: use handler Y
  • A, 2: use handler X

Syntax

function add(channel: Channel, handler: PermissionsHandler): Promise<void> | void
handler: function PermissionsHandler(channel: Channel, subscriber: Subscriber): Promise<ChannelPermissions> | ChannelPermissions

add Parameters

NAME
TYPE
DESCRIPTION
channel
Channel

Channel or channel resource to add a permissions handler for.

handler

The name of the function or the function expression to use when checking permissions for the specified channel.

Returns

Return Type:

Promise<void>

 | 

void

PermissionsHandler Parameters

NAME
TYPE
DESCRIPTION
channel
Channel

The channel to grant permissions to.

subscriber
Subscriber

The subscriber to grant permissions to.

Returns

Return Type:

Promise<ChannelPermissions>

 | 

ChannelPermissions

ChannelPermissions

ChannelPermissions

NAME
TYPE
DESCRIPTION
read
boolean

Whether the subscriber has read permissions. Defaults to true.

Was this helpful?

Set a permissions handler for a specific channel

Copy Code
1// In realtime-permissions.js
2
3import {permissionsRouter} from 'wix-realtime-backend';
4
5// ...
6
7const adminAnnouncements = {"name": "announcements"}
8
9permissionsRouter.add( adminAnnouncements, (channel, subscriber) => {
10 // add permissions check logic and return
11 // permissions for announcements channel
12 if(subscriber.type === "Admin") {
13 return {"read": true};
14 }
15 else {
16 return {"read": true};
17 }
18} );
Grant permissions for a channel based on subscriber type using the permissions router

Copy Code
1// In realtime-permissions.js
2
3import {permissionsRouter} from 'wix-realtime-backend';
4
5permissionsRouter.default( (channel, subscriber) => {
6 return {"read": true};
7} );
8
9const membersOnlyChannel = {"name": "MembersOnly"};
10
11permissionsRouter.add(membersOnlyChannel, (channel, subscriber) => {
12 if(subscriber.type === "Member") {
13 return {"read": true};
14 }
15 else {
16 return {"read": false};
17 }
18});
19
20export function realtime_check_permission(channel, subscriber) {
21 return permissionsRouter.check(channel,subscriber);
22}
Grant permissions based on user data using the permissions router

Copy Code
1// In realtime-permissions.js
2
3import { members } from `wix-members-backend`;
4import { permissionsRouter } from 'wix-realtime-backend';
5
6permissionsRouter.default((channel, subscriber) => {
7 return {"read": true};
8});
9
10const membersOnlyChannel = {"name": "MembersOnly"};
11
12permissionsRouter.add( membersOnlyChannel, async (channel, subscriber) => {
13 let member = await members.getMember(subscriber.id, { fieldsets: ['FULL'] });
14
15 if(channel.resourceId === "BobsOnly") {
16 if(member.contactDetails.firstName === "Bob") {
17 return { "read": true };
18 }
19 else {
20 return { "read": false };
21 }
22 }
23 else {
24 return { "read": true };
25 }
26} );
27
28export function realtime_check_permission(channel, subscriber) {
29 return permissionsRouter.check(channel, subscriber);
30}