Authorization

Authorization determines what an authenticated user is allowed to do within your application. While authentication verifies identity ('Who are you?'), authorization controls access ('What can you do?'). It ensures regular users can't access admin panels, free users can't use premium features, and users can only edit their own data.

Example

A user is authenticated (logged in), but when they try to access /admin/dashboard, the app checks their role. Regular users get redirected — only users with the 'admin' role are authorized to see that page.

Authentication gets users in the door. Authorization decides which rooms they can enter.

Authorization Patterns

Role-Based Access Control (RBAC)

Users have roles, roles have permissions:

RoleCan ViewCan EditCan DeleteCan Admin
UserOwn dataOwn dataOwn dataNo
EditorAll contentAll contentOwn contentNo
AdminEverythingEverythingEverythingYes

Resource-Based

Users can only access resources they own:

  • Users edit their own profile
  • Users see their own orders
  • Users delete their own comments

Where Authorization Lives

Authorization checks happen in multiple places:

  • API routes — Check permissions before processing requests
  • Middleware — Block unauthorized requests before they reach handlers
  • UI — Hide buttons and pages users can't access
  • Database queries — Filter results to only include authorized data

Common Authorization Mistakes

  • Client-side only — Hiding a button isn't security. Check permissions on the server too
  • Missing checks — Every API endpoint needs authorization, not just the UI
  • Overly permissive — Start with minimal permissions, add more as needed
  • Hardcoded roles — Use a flexible role system, not if/else chains

AI and Authorization

When asking AI to add authorization:

  • Specify the roles clearly
  • Define what each role can and can't do
  • Emphasize server-side checks
  • Ask for middleware-based solutions for consistency