Coder Social home page Coder Social logo

Comments (5)

daffl avatar daffl commented on June 2, 2024

checkPermissions will just return a hook function. You either have to call it with the context and await it or explicitly add it to the hook chain as recommended in the documentation:

get : [
  authenticate('jwt'),
  checkPermissions({
    roles: ['admin', 'user'],
    error:  false
  }),
  checkForAccess()
],

With a checkForAccess that calls the hook function like this:

const { restrictToOwner } =require('feathers-authentication-hooks');
const restrict = restrictToOwner({ idField:  'id', ownerField:  'id' });

module.exports = function (options  = {}) {
  return  async  context  => {
    if(!context.params.permitted) {
      return restrict(context);
    }
  }
};

from feathers-permissions.

greenspray9 avatar greenspray9 commented on June 2, 2024

wow thank you for your blazingly fast response! Apologies in advance if my question was not well formulated. I did read through the documentation and didn't know that it would return a hook function. I tried your method but context.params.permitted is still undefined.

from feathers-permissions.

daffl avatar daffl commented on June 2, 2024

With the example I gave I don't think there is a way it could be undefined. You could also change your original checkForAccess to:

const { restrictToOwner } = require('feathers-authentication-hooks');
const restrict = restrictToOwner({ idField:  'id', ownerField:  'id' });
const check = checkPermissions({
  roles: ['admin', 'user'],
  error:  false
});

module.exports = function (options  = {}) {
  return async ctx => {
    const context = await check(context);

    if(!context.params.permitted) {
      return restrict(context);
    }

    return context;
  }
};

from feathers-permissions.

greenspray9 avatar greenspray9 commented on June 2, 2024

thank you! that helped my case. I noticed that sometimes the context.params is just {} so i added another predicate to check for the size of the object. This is my updated code if anyone in the future also gets stuck in this

const checkPermissions = require('feathers-permissions');
const { restrictToOwner } = require('feathers-authentication-hooks');
const restrict = restrictToOwner({ idField:  'id', ownerField:  'id' });

const check = checkPermissions({
  roles: ['admin', 'user'],
  error:  false
});

module.exports = function (options  = {}) {
  return async context => {
    context = await check(context);
    if(Object.keys(context.params).length !== 0 && !context.params.permitted ) {  
      return restrict(context);
    }
    return context;
  };
};

from feathers-permissions.

daffl avatar daffl commented on June 2, 2024

Great, thank you for sharing.

from feathers-permissions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.