
Learn to build a rest api in NodeJS with MongoDB, including registration, login, and user crud. Hash passwords, implement Json web token authentication and role-based access, tested with Postman.
Install nodejs on Windows or Mac, verify with node -v, reinstall if needed; install VS Code and Postman, then prepare to set up the server in the next video.
Explore VS Code extensions for the project, including ES7 plus React Redux, Path and NPM IntelliSense, and ESLint, and learn to adjust editor zoom with the mouse wheel.
Create a Node.js auth project by initializing npm, installing express and nodemon, and building a server.js that uses dotenv, on port 5000, and reads MongoDB URL from .env.
Set up the MongoDB database with a Mongoose connection in a Node.js server, loading the URL from an env file and logging when connected.
Create a user model and schema with username, password, and role using mongoose, export the model, and distinguish between database and schema as preparation for CRUD operations.
Build a user registration flow in node.js by wiring a MongoDB connection, creating an async register function with password validation, and exposing the route via express router.
Create and export a login function that authenticates user credentials, validates username and password, queries the user database, and returns appropriate responses for success, not found, or errors.
update a user’s role to admin in a node.js and mongodb setup by finding the user by id, updating the role, and saving with error handling and status responses.
Implement a delete user function that removes a user by id from the database, returning a 201 status when successful or a 400 error with the message when it fails.
Hash the password during user registration using bcrypt with a moderate 10 rounds, store the hashed value in the database, and verify through a Postman registration example.
Hash the login password and verify it with bcrypt compare against the stored password, returning 200 login successful or 400 login not successful in Postman.
Create and send a JWT at user registration to shield routes from unauthenticated access, with a payload of username and role, and store the secret in env for cookie auth.
Generate a json web token for login by embedding the user id, username, and role, exclude the password, set expiry, and send the token via cookies and the login response.
Protect the private root by verifying the JWT from the cookie with a middleware, using cookie-parser, and allow only admins by checking the token's role.
Create a base user authentication using jwt tokens from cookies and verify with a secret, returning 401 on failure, and apply admin-only middleware to protect update and delete routes.
Build the app's front end with embedded JavaScript (EJS) and set the default view engine in server.js, creating routes for home, register, and login. Add admin route; run on 5000.
Create a registration page with a username and password form, including a login link, and post to /api/auth/register using fetch to handle responses, and redirect users by role after registration.
Easy, Simple, and step by step guide Course to Build Node.js Role based Authentication APIs with JWT and Consume in Frontend
This course is designed for everyone who wants to learn Rest API with JWT and role based authentication using Node.js.
What we are building in this course!
First, we create our project, setup up our server and create our user model, connect with MongoDB database then create simple registration and login function functionality, after than we perform crud operation on our user model. I will show you how you can hash registration and login password after that I will teach you how to add JWT authentication in Nodejs. I will also show you how to add role-based authentication and then we use Embedded javascript (ejs) to create frontend of the application. We also test our API with Postman.
Just follow along with me. I will holding your hand through the course. at the end of the course you have a solid understanding of role based authentication and how to use it to build a JWT authentication API that can you use a foundation to any project.
This course contain 14 section contain 23 video which over 4 hr 19 min long. you can watch at your own pace and at any point you get lost you can absolutely send a message. I Will happy to answer all your queries.