Authentication for TypeScript servers
Auth you can read in an afternoon.
Small modules for sessions, passwords, tokens and throttling, each tied to the attack it stops. Your routes, your status codes, your copy.
bun add @noctcore/auth
Pre-1.0: not ready for production yet, and the API may change until 1.0.
Every module answers an attack.
A request walks one path through the library. Each attack that comes at it stops at the module built for it. Every row links to the page that argues the rule.
- AttackForging a request from another site
http/csrf.tsAn Origin allowlist, Sec-Fetch-Site and a signed double-submit token. - AttackGuessing passwords against one account
throttle/Two attempt budgets, reserved before the hash runs. Locks are capped at an hour. - AttackLocking the owner out on purpose
throttle/device-token.tsA signed device cookie lets the owner's correct password through a lock. - AttackFinding out who has an account
flows/login.tsThe same answer after the same work, for known and unknown addresses. - AttackCracking a leaked password table
password/argon2.tsArgon2id at 19 MiB and t=3, rehashed on login when the parameters change. - AttackKeeping a stolen session after a password change
session/A revocation epoch fences out every session issued before it. - AttackClaiming an address before its owner signs up
flows/email-verification.tsVerification codes bound to the address they were sent to.
Your route, your status code.
createAuth wires the stores into the flows and checks the config once at startup. It hands back functions, not handlers.
- Your router reads the request and calls a flow.
- The flow returns
{ ok: true, value }or a refusal with a public code. - You choose the status, the cookie and the copy.
import { createAuth, toPublicError } from '@noctcore/auth';import { argon2id } from '@noctcore/auth/argon2';import { prismaStores } from '@noctcore/auth-prisma';import { redisStores } from '@noctcore/auth-redis';
const { users, tokens } = prismaStores({ delegate: prisma.user, tokenDelegate: prisma.authToken,});const { sessions, counters } = redisStores(redis);
export const auth = createAuth({ stores: { users, tokens, sessions, counters }, password: { hasher: argon2id() }, secret: process.env.AUTH_SECRET!, mailer,});
// In your own route handler:export async function login(form: LoginForm) { const result = await auth.login(form);
if (!result.ok) { // A wrong password, an unknown address and a locked account // all answer invalid_credentials, after the same work. return reply(statusFor(result.code), toPublicError(result)); }
return reply(200, { kind: result.value.kind });}What it refuses to do
- No exceptions for a user error
- Flows return results. A refusal carries a public
codethat never reveals whether an account exists, and an internalreasonkept for the audit trail. - No handlers or middleware
- The package never sees a request object. Your framework stays in charge of HTTP.
- No database of its own
- Stores are interfaces. Redis and Prisma ship today; any store that passes the conformance suite in
@noctcore/auth/testingworks. - No endpoints from plugins
- Plugins add hooks and flows under their own namespace, never a route.
- No mail on the response path
- Lock notices and reset mails run off the response path, or on your own queue.
Three packages
@noctcore/authThe core: sessions, passwords, tokens, throttling, CSRF and every flow. Pure TypeScript, no I/O of its own.@noctcore/auth-redisSession and counter stores on ioredis.@noctcore/auth-prismaUser and one-time token stores on Prisma, with the schema to merge.