Introduction to Authorization

Greetings! Today, we'll unlock the world of Authorization, with a focus on user roles, Role-Based Access Control (RBAC), and authorization middleware.

To illustrate Authorization simply—it's like a space mission, where only authorized astronauts can enter restricted areas. Now that we understand Authorization, let's implement it in our applications!

Understanding Authorization and Role-Based Access Control (RBAC)

Authorization comes into play post-authentication, deciding what actions authenticated users can perform. To break it down, Authentication is a spaceship's identity check, while Authorization is the spacesuit that guides users to access the various compartments of the spaceship.

Imagine a space facility with scientists and engineers, each having defined roles and access levels. Similarly, the Role-Based Access Control (RBAC) system assigns permissions based on roles.

Take an e-commerce system as an example:

  • Buyer views products.
  • Seller manages their products.
  • Admin controls the complete system.

RBAC is essential for managing large system!

Managing User Roles

In an authorization system, user roles dictate their system access. With MongoDB and Mongoose, we can manage user roles.

  1. Creating User Roles: Consider 'User' and 'Admin' roles. To oversee our system, we design a user schema with role as a property.
import mongoose from 'mongoose';

const UserSchema = mongoose.Schema({
    username: String,
    password: String,
    role: {
        type: String,
        default: 'User',
    },
});
  1. Assigning Permissions to Roles: Each role is given permissions. For instance, the 'User' role only allows the 'Read' operation, while the 'Admin' role can 'Read', 'Write', and 'Delete'.
  2. Assigning Users to Roles: Users are assigned roles upon signing up.
Establishing Authorization Middleware

Middleware in Express.js are vital functions that have access to the request and response objects, as well as the next middleware function in the application’s request-response cycle.

In the context of authorization, middleware come in particularly handy to check and verify the roles of users, in order to manage access rights to certain routes. Let's build an authorization middleware that will only allow 'Admin' role users to access a specific route.

const users = [
  { username: 'Alice', role: 'Admin' },
  { username: 'Bob', role: 'User' }
]; // An imaginary list of users for demonstration purpose

function checkifLoggedin(req) {
  return users[0];  // For the simplicity of this demo, we return the first user from our list, assuming they are logged in
} // Fake function just for the demonstration

function checkUserRole(req, res, next) {
  const user = checkifLoggedin(req);
  if (user.role !== 'Admin') {
    return res.status(403).send('You are not authorized to access this resource');
  }
  req.user = user; 
  return next(); 
}; // Middleware function to validate user roles

app.use('/admin', checkUserRole); // only apply this middleware to '/admin' route
app.get('/admin', function (req, res) {
  res.send(`Welcome ${req.user.username}! You are inside the Admin route`);
});

The checkUserRole middleware function checks if the logged-in user's role is 'Admin'. If so, it then transfers control to the next middleware function in the queue, which in this case is the route handler for '/admin'. If the user role isn't 'Admin', the server responds with a '403 Forbidden' status message.

Now, if you navigate to the /admin endpoint, only 'Admin' users will be able to access it. Anyone else will get a '403 Forbidden' message. As you continue building more complex applications, this mechanism of using middleware for role-based access control will become an essential aspect of maintaining application security. Welcome to an essential part of managing authorization in your web applications!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal