Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

12 Commits

Repository files navigation

Gate

A simple gate class that can help with protecting and checking user capabilities.

Install

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

Example

Define the gate rules

// 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
}

Using the gate rules

// 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')}}

About

A simple gate class that can help with protecting and checking user abilities.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages