Skip to content

feat: allow passing async ability function in channel options#61

Open
lukashass wants to merge 2 commits intofratzinger:mainfrom
geprog:async-channel-ability-update
Open

feat: allow passing async ability function in channel options#61
lukashass wants to merge 2 commits intofratzinger:mainfrom
geprog:async-channel-ability-update

Conversation

@lukashass
Copy link
Copy Markdown
Contributor

I'm trying to update the ability for channels by passing a function to channels.makeOptions:

const caslOptions = channels.makeOptions(app, {
  ability: (a, conn) => defineAbilitiesFor(conn.user, a),
});

Since defineAbilitiesFor is async (for querying data needed to create the ability, similar to #31), I added:

  • await before calling options.ability
  • call to getAbility when options.restrictFields === true

If there is a better way to update stale abilities for channels, any pointers in the right direction would be much appreciated.

@lukashass
Copy link
Copy Markdown
Contributor Author

@fratzinger Have you seen this? 😉

@fratzinger
Copy link
Copy Markdown
Owner

Thanks for the nudge and sorry for the delay!

If I get it right, making ability async is really not a good idea for channels cause it makes them pretty slow, I can assume.

You can override connection.ability at anytime.

For example, I do this:

  app.on("logout", (payload: any, { connection }) => {
    if (connection) {
      delete connection.ability;
      // you can also do something like:
      // connection.ability = await defineAbility(...);
      app.channel("anonymous").join(connection);
    }
  });

Does that help in any way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants