Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lukashass
Copy link
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
Contributor Author

@fratzinger Have you seen this? 😉

@fratzinger
Copy link
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.

None yet

3 participants