Implement role based access control, JWT, Express.js , Node.js

In this post, We gonna build an access control layer using express-jwt-permissions for express based application. ACL will keep users from accessing information not meant for them.

We gonna create the express.js application to role-based access control example.

let’s create a folder called aclJWTExpress

After creating, change the directory to aclJWTExpress

Now initialize the project with npm init command

--yes tells npm to use the default options while generating the package.json so you are not asked any questions

After running the command above, you should have a package.json file in the aclJWTExpress directory.

Below are the npm modules that we are going to use in this tutoiral
express – The popular Node framework
express-jwt-permissions – Permissions middleware for JWT tokens
body-parser – Will let us get parameters from our POST requests
jsonwebtoken – JSON Web Token sign and verification
sequelize – Sequelize is a promise-based ORM for Node.js v4 and up
sequelize-cli – sequelize-cli is usful to generate model, migration file..etc
mysql2 – MySQL client for Node.js with focus on performance

Install all the dependencies with below command

Initializes sequelize

Now create an empty project with sequelize cli init command. It will generate migrations, seeders, config and models directories, and defulat config file.

I got the following log in my console:

If you are using windows OS, open models/index.js file and correct the config file path.
Change the var config = require(__dirname + '/..\config\config.json')[env]; to var config = require(__dirname + '/../config/config.json')[env];

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

0 0 votes
Article Rating
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Georgi Iliev
Georgi Iliev
2 years ago

Is there a github repo where I can have a look at the code ? Protection Status
Would love your thoughts, please comment.x