Loading Depth Study
Preparing your workspace
Authentication verifies a user's identity, while authorization decides what that authenticated user is allowed to access or change.
Authentication answers the question 'Who are you?' and usually happens through credentials, sessions, tokens, or an identity provider. Authorization answers 'What are you allowed to do?' and depends on roles, permissions, ownership, or other application rules. A user can be authenticated but still not have permission to access a specific resource.
A student can log in successfully but should not automatically be allowed to open an admin dashboard.
if (!user) {
return unauthorized();
}
if (user.role !== 'admin') {
return forbidden();
}
return allowAccess();The application first checks whether the user is authenticated and then checks whether the user has the required role.