fix: harden production flows and user messaging
This commit is contained in:
1 parent
52dce168dd
commit
86dfe8053d
43 files changed
+3520
-358
No files matched your search
@@ -7,6 +7,12 @@ import { env } from "@/config/env";
|
||||
import { db } from "@/db/client";
|
||||
import { accounts, sessions, users, verifications } from "@/db/schema";
|
||||
import { mailer } from "@/modules/mail/mailer";
|
||||
import { consumeRateLimit, registrationPolicy } from "@/modules/security/rate-limit";
|
||||
import {
|
||||
enforceRateLimit,
|
||||
hashedRequestIdentity,
|
||||
type ConsumeRateLimit,
|
||||
} from "@/modules/security/rate-limit-guard";
|
||||
|
||||
import {
|
||||
createEditorialRegistrationRepository,
|
||||
@@ -14,6 +20,19 @@ import {
|
||||
type RegistrationInput,
|
||||
} from "./registration";
|
||||
|
||||
export const portalAuthRateLimitOptions = {
|
||||
enabled: true,
|
||||
storage: "memory",
|
||||
window: 60,
|
||||
max: 100,
|
||||
customRules: {
|
||||
"/sign-in/email": { window: 15 * 60, max: 5 },
|
||||
"/request-password-reset": { window: 60 * 60, max: 5 },
|
||||
"/reset-password": { window: 60 * 60, max: 5 },
|
||||
"/send-verification-email": { window: 60 * 60, max: 5 },
|
||||
},
|
||||
} satisfies NonNullable<BetterAuthOptions["rateLimit"]>;
|
||||
|
||||
export function createPortalAuth({
|
||||
database,
|
||||
mailerService,
|
||||
@@ -29,11 +48,16 @@ export function createPortalAuth({
|
||||
baseURL: env.BETTER_AUTH_URL,
|
||||
secret: env.BETTER_AUTH_SECRET,
|
||||
database,
|
||||
rateLimit: portalAuthRateLimitOptions,
|
||||
// All id columns are typed `uuid`; Better Auth's default id generator emits
|
||||
// a nanoid-style string that Postgres rejects with "invalid input syntax
|
||||
// for type uuid". Generate real UUIDs so inserts (signUp, sessions,
|
||||
// verifications) match the schema.
|
||||
advanced: { database: { generateId: () => crypto.randomUUID() } },
|
||||
advanced: {
|
||||
database: { generateId: () => crypto.randomUUID() },
|
||||
// The reverse proxy must overwrite this header; see docs/operations.md.
|
||||
ipAddress: { ipAddressHeaders: ["x-forwarded-for", "x-real-ip"] },
|
||||
},
|
||||
// `role` is a custom column on `users` (portalRole enum: ADMIN | EDITORIAL).
|
||||
// Better Auth's parseUserOutput filters the session user to declared fields
|
||||
// only, so without this the drizzle adapter selects `role` but it is then
|
||||
@@ -123,6 +147,24 @@ const portalAuth = createPortalAuth({
|
||||
export const auth = portalAuth.auth;
|
||||
export const registrationService = portalAuth.registrationService;
|
||||
|
||||
export function registerEditorial(input: RegistrationInput): Promise<{ status: "pending_email" }> {
|
||||
return registrationService.register(input);
|
||||
export function createRegistrationIngress({
|
||||
register,
|
||||
consumeRateLimit: consume,
|
||||
}: {
|
||||
register(input: RegistrationInput): Promise<{ status: "pending_email" }>;
|
||||
consumeRateLimit: ConsumeRateLimit;
|
||||
}) {
|
||||
return async function registrationIngress(input: RegistrationInput, headers = new Headers()) {
|
||||
await enforceRateLimit(
|
||||
consume,
|
||||
hashedRequestIdentity("registration", input.email, headers),
|
||||
registrationPolicy,
|
||||
);
|
||||
return register(input);
|
||||
};
|
||||
}
|
||||
|
||||
export const registerEditorial = createRegistrationIngress({
|
||||
register: (input) => registrationService.register(input),
|
||||
consumeRateLimit,
|
||||
});
|
||||
Reference in new issue
Block a user