Authentication - Axum - Complete Documentation by ntxm.org

Session-based auth

Learn what session-based auth is, how a cookie holding only a session id works, and when sessions fit your Axum app

Token-based auth

Learn what token-based auth is, how a client presents a token on every request, and how tokens differ from sessions

JWT auth approach

Learn what a JSON Web Token is, what its header payload and signature parts carry, and why expiry matters

Unopinionated Axum stance

Learn what Axum does and does not provide for auth, and why the framework leaves login session and token choices to your app

argon2 password hashing

Learn what a password hash is, why plain text and fast hashes fail, and how to hash and check a password with argon2 in Rust

Password hash column

Learn how to add a nullable password_hash column to the users table with a migration and keep the Rust User struct in step

Signup handler

Learn how a POST signup handler checks the input, hashes the password with argon2, inserts the user, and returns 201 without the hash

Password verify

Learn how to check a submitted password against the stored argon2 hash and answer every failure with the same generic 401

JWT login handler

Learn how a POST login handler checks an email and password against the database and returns a signed JWT when they match

JWT token claims

Learn what the Claims struct holds, what sub role and exp each mean, and why a token carries identity and expiry and nothing secret

JWT encoding key

Learn how EncodingKey from_secret turns a secret into a signing key, where the secret comes from, and why it must never live in code or in the repo

JWT decoding key

Learn how DecodingKey from_secret and Validation default verify a token signature and expiry, and what each verification failure means

AuthUser extractor

Learn how a FromRequestParts extractor reads the Authorization header, verifies the token, and hands handlers a typed AuthUser with a user id and a role

JWT Bearer token

Learn how the Bearer header scheme carries a JWT, how to read it and send it with curl, and what each malformed form receives

JWT protected route

Learn how a route that takes AuthUser as an argument only runs for requests with a valid token, and what an unauthenticated request receives

Auth middleware

Learn how to write an Axum auth middleware with middleware from_fn that verifies a JWT on every request and answers unauthenticated requests with 401

route_layer auth

Learn how to apply auth middleware to some routes and not others with route_layer in Axum and why route_layer keeps 404 and 405 responses intact

Admin-only route

Learn how to build an Axum admin-only route that returns 401 when the token is missing or bad and 403 when the user is known but not an admin

Role claim

Learn what the role claim inside a JWT is, how it is read after verification, and how it drives who may do what in an Axum app

CORS allowed origin

Learn what an origin is, why browsers block cross-origin reads, and how CorsLayer allow origin opens an Axum API to one named origin

CORS allowed methods

Learn which HTTP methods a browser may use cross-origin, how CorsLayer allow methods works, and what happens when a method is not on the list

CORS allowed headers

Learn which request headers a browser may send cross-origin, how CorsLayer allow headers works, and why a missing header fails the preflight

CORS credentials

Learn how cookies and Authorization headers travel cross-origin, how CorsLayer allow credentials enables them, and why credentials never go with a wildcard origin

tower-sessions crate

Learn what the tower-sessions crate provides, how a session store keeps session data, and how to wire SessionManagerLayer onto an Axum router

Session cookie on login

Learn how a login handler stores the user id in the session so the server sets a session cookie, and what that cookie holds

Session extractor in handlers

Learn how to read a session inside a handler with the Session extractor, get the stored user id back, and answer when the session is empty

Logout by clearing session

Learn how session flush ends a login on the server, why that matters more than deleting the cookie, and what happens to the old cookie

Session check middleware

Learn how to write a from_fn middleware that requires a session on protected routes and answers 401 when there is none

jsonwebtoken crate

Learn what the jsonwebtoken crate provides, how to add it to an Axum project, and how encode and decode turn claims into a signed token and back

Refresh token flow

Learn how short-lived access tokens pair with long-lived refresh tokens, how a client trades a refresh token for a new access token, and how rotation keeps stolen tokens useless

Avoid user enumeration in auth errors

Learn why a login must answer the same generic 401 whether the email is unknown or the password is wrong, and how different messages let an attacker list real accounts

401 vs 403 status choice

Learn the decision rule between 401 Unauthorized and 403 Forbidden, how the retry test settles every case, and which answer each kind of failed request gets

RequireRole extractor

Learn how to build a reusable RequireRole extractor that checks a token, verifies the role claim, and answers 401 for a bad token and 403 for the wrong role

bcrypt as password alternative

Learn how the bcrypt crate hashes and verifies passwords, how it differs from argon2, and when each one fits