Skip to content

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.

A login, with its reasons in viewA legitimate login continues through reserve attempt, verify password and the epoch fence to a session. Brute force and enumeration paths stop at the first two boundaries. A stolen session is rejected by the epoch fence after revocation. Simplified conceptual illustration, not executable flow code.

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.

  1. Your router reads the request and calls a flow.
  2. The flow returns { ok: true, value } or a refusal with a public code.
  3. You choose the status, the cookie and the copy.
src/auth.ts
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 code that never reveals whether an account exists, and an internal reason kept 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/testing works.
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.