Backend & Node.js · APIs, data and failure
What is the difference between authentication and authorization?
Short answer
Authentication proves who the caller is. Authorization decides what that caller may do. A request can be authenticated but still forbidden, which is why the two produce different errors and should be enforced at different layers.
Authentication happens first: validate a password, session or token and produce a principal. Authorization then checks that principal against a policy for the specific action and resource. Confusing the two leads to endpoints that verify identity but never check ownership.
function requireRole(role) {
return (req, res, next) => {
if (!req.user) {
return res.status(401).json({ error: "unauthenticated" });
}
if (!req.user.roles.includes(role)) {
return res.status(403).json({ error: "forbidden" });
}
next();
};
}Models you will meet
- RBAC assigns roles and checks role membership
- ABAC evaluates attributes such as department, region or resource owner
- ReBAC checks relationships, such as whether a user is an editor of a document
- Row-level and tenant-level checks must happen in the query, not only in the UI
- Central policy services help, but the resource server must still enforce the decision
Authorization belongs on the server and close to the data. Hiding a button is not access control. For every mutation, check the action and the specific resource, including ownership and tenant boundaries. A role check on the route is not enough if the record belongs to another customer.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1When should an API return 401 versus 403?
- 2What is the difference between RBAC and ABAC?
- 3Why is client-side hiding not authorization?
- 4How do you prevent cross-tenant data access?
Say it in the interview
Authentication establishes identity; authorization decides whether that identity may perform this action on this resource. I enforce authentication first, then check policy on the server for every operation, including ownership and tenant boundaries. Missing identity returns 401, while a valid identity that lacks permission returns 403. Hiding UI is never a substitute for a server-side check.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Backend & Node.js mock interview and get scored feedback on depth, clarity and confidence.