Search.../

default( )

Sets a permissions handler for default permissions.

Description

The handler set by the default() function is used for permission checks by the permissions router for all channels where an explicit handler has not been added. For channel resources, if no explicit handler has been set, the resource will inherit the permissions of its parent channel.

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

  • channel: A, permissions: no handler specified
  • channel: A, resource: 1, permissions: no handler specified
  • channel: A, resource: 2, permissions: handler is specified

Permissions will be checked as follows:

  • A: use the handler set by default() function
  • A, 1: use the handler set by default() function
  • A, 2: use the handler set by add() function for this resource

If no default handler is set, subscribers to channels without explicit permissions handlers will receive the following permissions: {"read": true}

Syntax

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

default Parameters

NAME
TYPE
DESCRIPTION
handler

The name of the function or the function expression to use when checking default permissions.

Returns

This function does not return anything.

Return Type:

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 default permissions handler

Copy Code
1// In realtime-permissions.js
2
3import {permissionsRouter} from 'wix-realtime-backend';
4
5// ...
6
7permissionsRouter.default( (channel, subscriber) => {
8 // return default permissions
9 return {"read": true};
10} );
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}