Why Authorization Differs from Authentication in Express
📖 Scenario: You are building a simple Express server that handles user login and access control. You want to understand the difference between authentication (checking who the user is) and authorization (checking what the user can do).
🎯 Goal: Create a basic Express app that authenticates a user by checking a username and password, then authorizes access to a protected route based on the user's role.
📋 What You'll Learn
Create an object called
users with usernames as keys and objects containing password and role as valuesCreate a variable called
loggedInUser initialized to nullWrite a function called
authenticate that takes username and password and sets loggedInUser if credentials matchWrite a function called
authorize that takes a role and returns true if loggedInUser has that roleAdd an Express route
/dashboard that uses authorize to allow access only if the user is an admin💡 Why This Matters
🌍 Real World
Web apps need to know who users are (authentication) and what they can do (authorization) to protect sensitive data and actions.
💼 Career
Understanding authentication and authorization is essential for backend developers building secure web services with Express.
Progress0 / 4 steps