Search.../

permissionsRouter

Returns the realtime permissions router.

Description

Use the permissions router to define the permissions you want to grant to subscribers on channels or channel resources. You can set permissions on the channel and channel resource level. If you do not define permissions for a particular channel resource, that resource inherits the permissions of its parent channel.

Typically you use the permissions router in the realtime-permissions.js file in your site's backend to:

  • Set default permissions for all channels and channel resources where no explicit permissions are defined.
  • Add permissions logic for specific channels or channel resources.
  • Check the permissions for a specific subscriber on a specific channel.

Note: Although it is recommended, you do not need to use the permissions router to set permissions. You can also define all your permissions logic in the realtime_check_permission() function.

Type:

Was this helpful?

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 wixUsersBackend from `wix-users-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 user = await wixUsersBackend.getUser(subscriber.id);
14
15 if(channel.resourceId === "BobsOnly") {
16 if(user.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}