Create a new Service with casl&Dashboard

You can create new service with a cli or manual:

we use hygen to generate new service

Add hygen

1 - download this repo 2 - copy this _templates folder to your app 3 - install hygen npm i hygen --save-dev 4 - inside your src/service/index.js after the last app.configure(..) add comment // services module.exports = function (app) { .... app.configure(services.userAbilities); // services } 5- add this script to your package.json "create-service": "hygen generator service" Done!

Create new service run

npm run create-service

✔ Name of your service · posts

inject: src/services/index.js added: src/services/posts/posts.class.js added: src/services/posts/posts.hooks.js added: src/services/posts/posts.model.js added: src/services/posts/posts.service.js added: src/services/posts/posts.validators.js

Hooks in the service:

  • hook.authenticate @feathersjs/authentication - Feathers local, token, and OAuth authentication over REST and Websockets using JSON Web Tokens (JWT) with PassportJS.

  • validateAbilities This is a wrapper of Casl, in this hook, we will define abilities and block client without the ability to run this request Casl will add to mongoose query object a relevant key value before making the request, and validate Abilities will remove fields from user request by id abilities

  • validateSchema This hook will use JOI to validate request data follow the scheme

  • sanitizedData This hook will remove data from response follow the user abilities

7. Commit changes

git add .
git commit -m "create posts service"

dashboard:

Now you can see the posts service inside the dashboard https://feathersjs-mongoose-casl-admin.herokuapp.com/ Anyone can read the posts title User can create/update only if he the author Only admin user can delete posts

Try to create a new posts from the dashboard

Last updated