Something went wrong. Try again.
A free, open-source, self-hosted place to write a novel.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import { and, eq } from 'drizzle-orm';import { db, tables } from './db/index.ts';
/* * A reset link is good only for the password it was asked for. better-auth spends * the token it is handed and leaves every other row for that writer alone, and the * three password paths here — the reset form, the account page and the console — * never swept them, so a link mailed before a password changed kept working for * the rest of its hour. * * The comparison below is the rule instead of a sweep. better-auth stamps * `account.updated_at` on every password write and `scripts/ops.mjs` sets it by * hand, so a link older than the last write is stale however the password moved. * Nothing has to be undone on a password path, so a password path cannot forget, * and there is no window between a password write and a cleanup that failed. */const RESET_PREFIX = 'reset-password:';
/** * Whether a mailed reset token can no longer set a password. True for a token this * instance never stored, one past its expiry, and one older than the writer's last * password change. */export function resetTokenStale(token: string): boolean { const row = db .select() .from(tables.verification) .where(eq(tables.verification.identifier, `${RESET_PREFIX}${token}`)) .get();
if (!row) return true;
if (row.expiresAt.getTime() < Date.now()) return true;
const credential = db .select({ updatedAt: tables.account.updatedAt }) .from(tables.account) .where(and(eq(tables.account.userId, row.value), eq(tables.account.providerId, 'credential'))) .get();
/* * An account with no password row yet is mid-recovery rather than stale: the * reset is what creates that row, so there is no earlier password to outdate it. * The comparison is inclusive because both stamps are whole milliseconds, and a * link asked for in the same millisecond as the write it follows is still good. */ if (!credential) return false;
return row.createdAt.getTime() < credential.updatedAt.getTime();}