Skip to content

Authentication and authorization

Introduction

Authentication verifies identity; authorization checks permissions. The two must be done separately, and neither can be skipped.

✗ Without it
User signs in successfully
Change the order id in the URL to someone else's
Server only checks login state
See someone else's order
✓ With it
User signs in successfully
Change the order id in the URL to someone else's
Server also checks whether that order belongs to you
Deny access

Why keep them separate

The most common real vulnerability is not “access without logging in”, but after logging in, accessing other people’s data. For example, changing /orders/123 in the URL to /orders/124 and seeing someone else’s order. That is called broken access control (IDOR): authentication was done, authorization was not.

How to do it

Authentication

  1. Store passwords with argon2 / bcrypt salted hashes; never store plaintext, and do not use MD5/SHA1.
  2. After login, issue a session credential: a cookie session or a JWT.
  3. Prefer ready-made solutions (Identity, Django auth, third-party sign-in) over building your own.

Authorization

  1. Declare explicitly on every endpoint who can access it (role / permission / resource owner).
  2. Deny by default: if it is not declared, do not allow it.
  3. When querying data, include a “belongs to the current user” condition.

Component comparison

ASP.NET Core Identity

Built into the frameworkOpsLow

.NET's built-in user system: registration, login, password encryption, and password recovery all have ready-made code, with user data stored in your own database.

Best forA single C# web application or backend, with user data in your own database.

Avoid whenYou need unified login across several systems, or nobody on the team wants to maintain the security details.

Pros

Deeply integrated with ASP.NET Core and works out of the box

User data is in your own database, with no dependence on external services

Common features such as password hashing, account lockout, and two-factor authentication are built in

Cons

The UI for login pages, email verification, and password recovery must be built yourself

Only manages this one system; sharing one set of accounts across multiple systems requires building an extra service

Social login and SMS login must be integrated separately

Official docs ↗

Django auth / FastAPI Users

Built into the frameworkOpsLow

Ready-made user systems in Python frameworks: Django ships accounts and permissions, and FastAPI can use FastAPI Users, a community library.

Best forA single Python web application or internal backend.

Avoid whenYou need unified login across systems, or many third-party login methods.

Pros

Django includes users, passwords, and the admin

User data stays in your own database, and fields can be extended

It is free to use and does not depend on an external identity service

Cons

Django auth is tightly bound, so changing frameworks means a rewrite

FastAPI Users is maintained at the pace of its community author

Social login, SMS, and sharing accounts across systems are yours to add

Official docs ↗

Keycloak

Self-hosted open sourceOpen sourceOpsHigh

A standalone "identity service": it handles login, registration, and single sign-on, and your app only asks it "who is this person".

Best forTeams with multiple systems needing unified login, or whose data must stay on their own servers.

Avoid whenThere is only one small app and nobody responsible for ops.

Pros

Login pages, user management console, social login, and two-factor are all ready-made

Multiple systems can share one set of accounts

Follows common standards, with integration options in every language

Cons

You must deploy a memory-hungry service yourself and upgrade and maintain it long term

Many settings, and troubleshooting requires understanding identity protocols

Making the login page match your own style needs extra customization

Official docs ↗

Managed identity service (Auth0, Clerk, etc.)

Managed serviceFree tier + paidOpsLow

Hand login over to a specialist company: you embed their login components, and user accounts are stored with them too.

Best forWeb applications that want to launch quickly and whose users are mostly overseas.

Avoid whenUsers are mostly in China and data must not leave the country, or the user base is huge and the budget tight.

Pros

You can connect it in a few hours and do not manage passwords yourself

Social login, verification codes, and two-factor are basically all there

The provider runs and upgrades it, so you do not operate it

Cons

As users grow, usage billing makes the bill rise noticeably

Data sits with an overseas provider, so compliance and access need a review

Moving away later makes exporting accounts and passwords awkward

Official docs ↗

China cloud identity services (Alibaba Cloud IDaaS, Authing, etc.)

Managed serviceFree tier + paidOpsLow

Managed login services from Chinese vendors, supporting login methods commonly used in China such as mobile SMS, WeChat, WeCom, and DingTalk.

Best forApplications aimed at Chinese users that need mobile number or WeChat login.

Avoid whenUsers are mostly overseas, or you want complete control over account data.

Pros

Domestic login methods are well supported, and data can stay in China

The vendor hosts the identity service, so you do not operate it

The console, the docs, and technical support are all in Chinese

Cons

Each vendor's API and pricing differ, so switching means code changes

Free quotas and feature tiers follow the vendor's policy at the time

Community tutorials are fewer than for products such as Auth0

Axum extractor auth

Open-source libraryOpen sourceOpsLow

An Axum extractor reads the session or JWT and finishes authentication and authorization before business code runs.

Best forLogin and permissions for any Axum project.

Avoid whenAuthentication is fully delegated to an external identity service and the gateway authorizes (still validate the token).

Pros

Auth failures return from the extractor, so routes stay clean

Sessions can use tower-sessions, or you can issue JWTs instead

It can be stacked together with Tower middleware

Cons

Registration, CSRF, and the login flow all have to be assembled by you

A route that forgets the extractor will let the request through

Sessions on multiple instances must live in shared storage

Official docs ↗

Passport

Open-source libraryOpen sourceOpsLow

The classic authentication middleware for Node.js, using "strategies" to support local username/password, OAuth, JWT, and other login methods.

Best forNode backends that need to control their own account system and offer many login methods.

Avoid whenYou want accounts, sessions, and multi-factor authentication out of the box (consider a managed identity service or Keycloak).

Pros

Almost every common login method already has a ready-made strategy

Nest, Express, and Fastify can all integrate it

Switching login method does not require a big change in how you use it

Cons

It only covers authentication; sessions and permissions are yours to build

Strategy packages vary a lot in quality and in how well they are maintained

Type declarations are incomplete, so you often fill them in yourself

Official docs ↗

Better Auth

Open-source libraryOpen sourceOpsLow

A framework-agnostic TypeScript authentication library with built-in accounts, sessions, OAuth, and multi-factor support, attachable to many backends and full-stack frameworks.

Best forTypeScript full-stack projects that want a complete account system quickly.

Avoid whenCompliance requires a mature, long-proven identity system (consider Keycloak and similar).

Pros

Accounts, sessions, social login, and multi-factor are all included

It is type-safe and can connect to ORMs such as Prisma

Next.js, Nuxt, and Hono can all use it

Cons

The project is still young, so keep watching the API for changes

Deep customization means reading the source and the plugin system

Judge how much security review it has had, and whether it will stay maintained

Official docs ↗

scs session management

Open-source libraryOpen sourceOpsLow

An HTTP session management library for Go, supporting cookies and multiple storage backends (memory, Redis, SQL), used alongside the standard library.

Best forGo applications that are server-side rendered or rely mainly on cookie sessions.

Avoid whenApps / third parties are the main clients and you need stateless tokens (use a JWT approach).

Pros

Fully compatible with net/http and almost non-invasive

Multiple storage backends, with ready-made Redis / PostgreSQL implementations

Provides session renewal and token rotation

Cons

Only handles sessions; password verification, the account system, and permissions are yours to implement

You must implement login rate limiting and CSRF protection yourself

Multi-instance deployment requires shared storage

Official docs ↗

Checklist

  • Passwords are hashed with argon2/bcrypt
  • Endpoints require login by default; public ones are marked explicitly
  • Every resource accessed by id checks ownership
  • Tokens expire, and can be invalidated after logout or password change
  • The login endpoint has rate limiting (see Rate limiting)