mirror of
https://github.com/nestriness/nestri.git
synced 2026-09-19 17:25:19 +03:00
feat: bring the control plane up to date
Squashes the current state of the internal working tree onto this history. The two trees had grown apart with no common ancestor, so this is a content sync rather than a merge, and the published history is preserved rather than rewritten — a force-push here would break every existing fork and clone to no benefit. What lands: - Waitlist: API route, core module, and migration 0006 alongside game aliases. - User verification. - CI, oxfmt config, editor settings. - Assorted fixes across the API routes and core modules. The repository's own README, the wordmark and the per-package READMEs are kept from this side; the internal tree had dropped them and they are what a stranger arriving here reads first. The marketing site in the internal tree is deliberately not here. It is a separate product with its own repo and its own licence, and this repo is the open one — a closed component does not belong in it regardless of how convenient the directory looked. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -56,6 +56,16 @@ export namespace Fingerprint {
|
||||
}
|
||||
);
|
||||
|
||||
export const fromID = fn(Info.shape.id, async (id) => {
|
||||
return Database.use(async (tx) => {
|
||||
return tx
|
||||
.select()
|
||||
.from(UserFingerprintTable)
|
||||
.where(and(eq(UserFingerprintTable.id, id), isNull(UserFingerprintTable.timeDeleted)))
|
||||
.then((rows) => rows.at(0) ?? null);
|
||||
});
|
||||
});
|
||||
|
||||
export const findByFingerprint = fn(Info.shape.fingerprint, async (fingerprint) => {
|
||||
return Database.use(async (tx) => {
|
||||
return tx
|
||||
|
||||
@@ -103,6 +103,23 @@ export namespace User {
|
||||
}
|
||||
);
|
||||
|
||||
export const setEmail = fn(
|
||||
Info.pick({ id: true }).extend({ email: z.email(), emailVerified: z.boolean() }),
|
||||
async (input) => {
|
||||
return Database.use(async (tx) => {
|
||||
const [row] = await tx
|
||||
.update(UserTable)
|
||||
.set({
|
||||
email: input.email,
|
||||
emailVerified: input.emailVerified
|
||||
})
|
||||
.where(eq(UserTable.id, input.id))
|
||||
.returning();
|
||||
return row ? serialize(row) : null;
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
export const remove = fn(Info.shape.id, async (id) => {
|
||||
await Database.use(async (tx) => {
|
||||
await tx
|
||||
|
||||
30
packages/core/src/user/verification.sql.ts
Normal file
30
packages/core/src/user/verification.sql.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { index, integer, pgEnum, pgTable, text } from 'drizzle-orm/pg-core';
|
||||
|
||||
import { id, timestamps, ulid, utc } from '../db/types.js';
|
||||
import { UserTable } from './user.sql.js';
|
||||
|
||||
export const VerificationKindEnum = pgEnum('verification_kind', ['email']);
|
||||
|
||||
/**
|
||||
* A short-lived code proving the owner of an email address.
|
||||
*
|
||||
* The `ver` id prefix was reserved for this before the table existed. Codes
|
||||
* are hashed at rest so a leaked database cannot be used to verify on behalf
|
||||
* of its users.
|
||||
*/
|
||||
export const VerificationTable = pgTable(
|
||||
'verification',
|
||||
{
|
||||
...id,
|
||||
...timestamps,
|
||||
userId: ulid('user_id')
|
||||
.notNull()
|
||||
.references(() => UserTable.id, { onDelete: 'cascade' }),
|
||||
kind: VerificationKindEnum('kind').notNull(),
|
||||
codeHash: text('code_hash').notNull(),
|
||||
expiresAt: utc('expires_at').notNull(),
|
||||
attempts: integer('attempts').notNull().default(0),
|
||||
consumedAt: utc('consumed_at')
|
||||
},
|
||||
(t) => [index('verification_user_kind_idx').on(t.userId, t.kind)]
|
||||
);
|
||||
146
packages/core/src/user/verification.ts
Normal file
146
packages/core/src/user/verification.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
|
||||
import { and, desc, eq, gt, isNull, sql } from 'drizzle-orm';
|
||||
import z from 'zod';
|
||||
|
||||
import { Database } from '../db/index.js';
|
||||
import { fn } from '../fn.js';
|
||||
import { Identifier } from '../id.js';
|
||||
import { UserTable } from './user.sql.js';
|
||||
import { VerificationKindEnum, VerificationTable } from './verification.sql.js';
|
||||
|
||||
export function hashCode(code: string): string {
|
||||
return createHash('sha256').update(code).digest('hex');
|
||||
}
|
||||
|
||||
function generateCode(): string {
|
||||
const bytes = randomBytes(3);
|
||||
return String((bytes[0]! << 16) | (bytes[1]! << 8) | bytes[2]!)
|
||||
.padStart(6, '0')
|
||||
.slice(0, 6);
|
||||
}
|
||||
|
||||
export const MAX_VERIFICATION_ATTEMPTS = 5;
|
||||
export const VERIFICATION_TTL_MINUTES = 10;
|
||||
|
||||
export namespace Verification {
|
||||
export const create = fn(
|
||||
z.object({
|
||||
userId: z.string(),
|
||||
kind: z.enum(VerificationKindEnum.enumValues),
|
||||
code: z.string().optional()
|
||||
}),
|
||||
async (input) => {
|
||||
const code = input.code ?? generateCode();
|
||||
// Old codes must not stay valid once a fresh one exists.
|
||||
await Database.use(async (tx) => {
|
||||
await tx
|
||||
.update(VerificationTable)
|
||||
.set({ consumedAt: sql`now()` })
|
||||
.where(
|
||||
and(
|
||||
eq(VerificationTable.userId, input.userId),
|
||||
eq(VerificationTable.kind, input.kind),
|
||||
isNull(VerificationTable.consumedAt)
|
||||
)
|
||||
);
|
||||
await tx.insert(VerificationTable).values({
|
||||
id: Identifier.ascending('verification'),
|
||||
userId: input.userId,
|
||||
kind: input.kind,
|
||||
codeHash: hashCode(code),
|
||||
expiresAt: sql`now() + interval '${sql.raw(String(VERIFICATION_TTL_MINUTES))} minutes'`,
|
||||
attempts: 0,
|
||||
consumedAt: null
|
||||
});
|
||||
});
|
||||
return code;
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Redeem a code for the user's email.
|
||||
*
|
||||
* The whole flow — find the active code, check the hash, burn the code,
|
||||
* flip the flag — happens in one transaction so a code cannot be raced
|
||||
* into double use.
|
||||
*/
|
||||
export const verifyEmail = fn(
|
||||
z.object({ userId: z.string(), code: z.string() }),
|
||||
async (input) => {
|
||||
return Database.transaction(async (tx) => {
|
||||
const active = await tx
|
||||
.select()
|
||||
.from(VerificationTable)
|
||||
.where(
|
||||
and(
|
||||
eq(VerificationTable.userId, input.userId),
|
||||
eq(VerificationTable.kind, 'email'),
|
||||
isNull(VerificationTable.consumedAt),
|
||||
gt(VerificationTable.expiresAt, sql`now()`)
|
||||
)
|
||||
)
|
||||
.orderBy(desc(VerificationTable.timeCreated))
|
||||
.then((rows) => rows.at(0) ?? null);
|
||||
|
||||
if (!active) {
|
||||
return { ok: false as const, reason: 'no_active_code' as const };
|
||||
}
|
||||
|
||||
if (hashCode(input.code) !== active.codeHash) {
|
||||
const burn = active.attempts + 1 >= MAX_VERIFICATION_ATTEMPTS;
|
||||
await tx
|
||||
.update(VerificationTable)
|
||||
.set({
|
||||
attempts: sql`${VerificationTable.attempts} + 1`,
|
||||
// Exhausted codes are consumed so the next attempt
|
||||
// asks for a fresh one instead of counting forever.
|
||||
consumedAt: burn ? sql`now()` : undefined
|
||||
})
|
||||
.where(eq(VerificationTable.id, active.id));
|
||||
return { ok: false as const, reason: 'wrong_code' as const };
|
||||
}
|
||||
|
||||
await tx
|
||||
.update(VerificationTable)
|
||||
.set({ consumedAt: sql`now()` })
|
||||
.where(eq(VerificationTable.id, active.id));
|
||||
await tx
|
||||
.update(UserTable)
|
||||
.set({ emailVerified: true })
|
||||
.where(eq(UserTable.id, input.userId));
|
||||
return { ok: true as const };
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
export const findActiveByUserAndKind = fn(
|
||||
z.object({ userId: z.string(), kind: z.enum(VerificationKindEnum.enumValues) }),
|
||||
async (input) => {
|
||||
return Database.use(async (tx) => {
|
||||
return tx
|
||||
.select()
|
||||
.from(VerificationTable)
|
||||
.where(
|
||||
and(
|
||||
eq(VerificationTable.userId, input.userId),
|
||||
eq(VerificationTable.kind, input.kind),
|
||||
isNull(VerificationTable.consumedAt),
|
||||
gt(VerificationTable.expiresAt, sql`now()`)
|
||||
)
|
||||
)
|
||||
.orderBy(desc(VerificationTable.timeCreated))
|
||||
.then((rows) => rows.at(0) ?? null);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
export const consume = fn(z.string(), async (id) => {
|
||||
await Database.use(async (tx) => {
|
||||
await tx
|
||||
.update(VerificationTable)
|
||||
.set({ consumedAt: sql`now()` })
|
||||
.where(eq(VerificationTable.id, id));
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user