A simple gate class that can help with protecting and checking user capabilities.
This package is made to run in your backend because checking the gate access should be done in the backend. But it can be used on the client side. For example to show or hide ui elements or protect client side routes. It is based on the gate functionality in Laravel
npm install @stackkit/gate
yarn add @stackkit/gate
// userGate.jsconst{ Gate }=require('gate')functiongate({ user }){constgate=newGate({ user })// run code before checking every other gategate.before(({ user })=>{returnuser.role==='god'})// define a gategate.define('edit-users',({ user })=>{returnuser.role==='admin'})gate.define('edit-post',({ user, post })=>{returnpost.created_by===user.id})// run code after done checking every other gategate.after(({ user })=>{returnuser.namespaces.length>0})returngate}module.exports={
gate
}// your request handlerconst{ gate }=require('./gates/userGate')functionhandleRequest(req){constgate=useGate({ user })if(gate.allows('edit-users')){console.log('user is allowed to edit users')}constpost=prisma.findFirst({where: {id: req.params.id}})if(gate.check('edit-post',{ post })){console.log('user is allowed to edit the given post')}}