Authentication
@dotpress/auth authenticates the users of your API with a username and a password, and keeps them logged in with short-lived access tokens and rotating refresh tokens. It works with any database, through a small data provider, and on every adapter.
What it gives you:
- the routes: login, token refresh, current user, cookie session restore, logout;
ctx.userandctx.auth, typed, in every handler, and guards:requireAuth,requireRole(),requireClient();- sessions for browsers in
httpOnlycookies, protected against cross-site request forgery; - security by default: refresh token rotation with reuse detection, login throttling, constant-time login failures, bcrypt hashing, RFC 9068 access tokens checked on every request.
How it works
- The client logs in with a username and a password. It gets an access token, valid 15 minutes, and a refresh token.
- It sends the access token with each request. The plugin verifies it, loads the user, and sets
ctx.user. - Before the access token expires, the client exchanges the refresh token for a new pair. The old refresh token can no longer be used: a stolen one is detected when it comes back.
- A logout revokes the session.
The access token is a JWT signed with your RSA key: verifying it costs no database lookup of the token. The refresh token is a random value; only its SHA-256 hash is stored, in your database.
Install
Install @dotpress/auth with the alpha tag: without it, npm installs @dotpress/auth 1.x, the plugin of Dotpress 1.
Set up
Describe your user, create the auth service, and install its plugin:
See Data provider to connect your database, and Keys and rotation to generate the keys.
The routes
Each route answers with userId, expiresIn and expiresAt (the expiration of the access token), so that the client knows when to refresh. The tokens are in the body, or in cookies (see below).
authPlugin(auth, { prefix: '/api/auth' }) changes the prefix; { routes: false } keeps only the authentication of the requests, for an API that only verifies tokens.
Browsers or other clients
The same API serves two kinds of clients:
Use the cookie mode with accessTokenInCookie. A token stored in localStorage, or in any variable of the page, can be stolen by a single injected script (XSS); an httpOnly cookie cannot. See Cookie mode and CSRF.
Without an app
The auth service works in scripts and workers too:
In this section
- Data provider: connect your database.
- Protecting routes: guards, roles, the type of the user.
- Sessions and tokens: lifetimes, rotation, logout, revocation.
- Cookie mode and CSRF: sessions for browsers.
- Clients: several applications on one API.
- Passwords and brute force.
- Keys and rotation.
- Security checklist: what to check before going to production.
- Auth reference: every setting, route, error and contract.
The Auth example puts it all together: sign-up, both modes, rotation, logout of one or every device, password change, roles.
configurePlugin() is replaced by createAuth() and authPlugin(auth). Access tokens follow RFC 9068, issuer is required, logout revokes the current session only, login attempts are throttled, and tokens set as cookies are no longer returned in the body. See Migrate from v1.