Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | 35x 35x 593x 593x 593x 593x 593x 593x 593x 593x 593x 8x 8x 17x 17x 8x 8x 2x 15x 13x 291x 8x 25x 250x 250x 2x 248x 248x | import { Email } from './email.vo';
import { InvalidNameError } from './errors/invalid-name.error';
/** How long a demo account lives before the hourly sweep deletes it, with everything it holds. */
export const DEMO_ACCOUNT_LIFETIME_MS = 24 * 60 * 60 * 1000;
/**
* The domain every demo account's address is built on. `.invalid` is reserved by RFC 2606 and can
* never resolve, so nothing addressed there can be delivered — not by us, and not by anyone who
* copies the address off a demo profile into another form.
*/
export const DEMO_EMAIL_DOMAIN = 'demo.cygnus.invalid';
export interface UserProps {
id: string;
email: string;
passwordHash: string;
avatarUrl?: string | null;
name: string;
emailNotificationsEnabled: boolean;
createdAt: Date;
sessionVersion: number;
demoExpiresAt?: Date | null;
}
export class User {
readonly id: string;
readonly email: string;
readonly passwordHash: string;
readonly avatarUrl: string | null;
readonly name: string;
readonly emailNotificationsEnabled: boolean;
readonly createdAt: Date;
/**
* Incremented to end every session this account has open — refresh tokens carry the version they
* were minted under, so bumping it invalidates all of them at once without enumerating any.
*/
readonly sessionVersion: number;
/** When the sweep deletes this account; null for every account a person actually owns. */
readonly demoExpiresAt: Date | null;
private constructor(props: UserProps) {
this.id = props.id;
this.email = props.email;
this.passwordHash = props.passwordHash;
this.avatarUrl = props.avatarUrl ?? null;
this.name = props.name;
this.emailNotificationsEnabled = props.emailNotificationsEnabled;
this.createdAt = props.createdAt;
this.sessionVersion = props.sessionVersion;
this.demoExpiresAt = props.demoExpiresAt ?? null;
}
get isDemo(): boolean {
return this.demoExpiresAt !== null;
}
/**
* The same account with a new password and every older session ended: the session version moves
* on, so the refresh tokens minted before the reset stop working.
*
* Built from this instance, not through `create`. The account already exists, and `create` holds
* the rules for a *new* one: run on an account opened by a sign-in code before its owner gave a
* name (`createUnnamed`), it refused the empty name and the reset answered 500.
*/
withResetPassword(passwordHash: string): User {
return new User({
id: this.id,
email: this.email,
passwordHash,
avatarUrl: this.avatarUrl,
name: this.name,
emailNotificationsEnabled: this.emailNotificationsEnabled,
createdAt: this.createdAt,
sessionVersion: this.sessionVersion + 1,
demoExpiresAt: this.demoExpiresAt,
});
}
/**
* The same account with a profile edit applied. Only what changes is checked: a new name must not
* be blank and a new address must be an e-mail. An account opened by a sign-in code before its
* owner gave a name (`createUnnamed`) can therefore still change its photo or its e-mail
* preferences; rebuilding it through `create` re-checked the stored empty name and refused them.
*
* The session version and the demo expiry are carried over: rebuilding the entity without the
* first would bring back every session a password reset had ended, and the repository never
* updates the second, so keeping it only keeps the returned entity truthful.
*/
withProfile(changes: {
name?: string;
email?: string;
passwordHash?: string;
avatarUrl?: string | null;
emailNotificationsEnabled?: boolean;
}): User {
let name = this.name;
if (changes.name !== undefined) {
name = changes.name.trim();
if (name.length === 0) {
throw new InvalidNameError();
}
}
return new User({
id: this.id,
email: changes.email !== undefined ? Email.create(changes.email).toString() : this.email,
passwordHash: changes.passwordHash ?? this.passwordHash,
avatarUrl: changes.avatarUrl !== undefined ? changes.avatarUrl : this.avatarUrl,
name,
emailNotificationsEnabled: changes.emailNotificationsEnabled ?? this.emailNotificationsEnabled,
createdAt: this.createdAt,
sessionVersion: this.sessionVersion,
demoExpiresAt: this.demoExpiresAt,
});
}
/** True once a demo account has outlived its 24 hours, even if the sweep has not reached it yet. */
isExpiredDemoAt(now: Date): boolean {
return this.demoExpiresAt !== null && this.demoExpiresAt.getTime() <= now.getTime();
}
/**
* Rebuilds an entity that is already in the database.
*
* **Not `create`.** That one enforces the rules for making a *new* user, and running them again
* on a row that already exists says that a record the system itself wrote cannot be read back —
* which is how an unnamed account (see `createUnnamed`) turned every `GET /auth/me` into a 500
* until this existed. Reconstitution trusts the database, because by then the database is the
* record; validation belongs at the door, once.
*/
static fromPersistence(props: UserProps): User {
return new User(props);
}
/**
* An account that exists before anybody has said what to call its owner.
*
* Passwordless sign-in creates the account from a verified code, and the name cannot be asked
* for at that moment: the request screen only knows an address, and asking for a name *only*
* when the address is unknown would answer the question that flow is built to refuse — whether
* this address has an account here. So it is asked after the session exists, by the frontend,
* where it reveals nothing.
*
* A separate factory rather than a looser `create`: "a user has a name" stays true of every
* account that came through the sign-up form, and the exception is visible at the one call site
* that takes it, instead of being an invariant quietly deleted for everybody.
*/
static createUnnamed(props: { id: string; email: string; passwordHash: string }): User {
return new User({
id: props.id,
email: Email.create(props.email).toString(),
passwordHash: props.passwordHash,
name: '',
emailNotificationsEnabled: true,
createdAt: new Date(),
sessionVersion: 0,
});
}
/**
* The disposable account behind "try the demo": a recruiter or a curious parent sees the app full
* of data without handing over an address, and the account deletes itself a day later.
*
* Everything that would tie it to a person is absent on purpose. The address is synthetic and
* undeliverable (`DEMO_EMAIL_DOMAIN`), reminder e-mails are off, and the password hash is derived
* by the caller from random bytes, so no password, code or link can sign into it again — or turn
* it into a permanent account, because changing the address or the password asks for the current
* password. The session handed out at creation is the only way in.
*
* It does get a name, unlike `createUnnamed`: the frontend asks an unnamed account what to call
* its owner, and a demo should open on the product, not on a form.
*/
static createDemo(props: { id: string; passwordHash: string; name: string; now: Date }): User {
return new User({
id: props.id,
email: Email.create(`demo-${props.id}@${DEMO_EMAIL_DOMAIN}`).toString(),
passwordHash: props.passwordHash,
name: props.name,
emailNotificationsEnabled: false,
createdAt: props.now,
sessionVersion: 0,
demoExpiresAt: new Date(props.now.getTime() + DEMO_ACCOUNT_LIFETIME_MS),
});
}
static create(props: {
id: string;
email: string;
passwordHash: string;
avatarUrl?: string | null;
name: string;
emailNotificationsEnabled?: boolean;
createdAt?: Date;
sessionVersion?: number;
demoExpiresAt?: Date | null;
}): User {
const name = props.name.trim();
if (name.length === 0) {
throw new InvalidNameError();
}
const email = Email.create(props.email).toString();
return new User({
id: props.id,
email,
passwordHash: props.passwordHash,
name,
avatarUrl: props.avatarUrl ?? null,
emailNotificationsEnabled: props.emailNotificationsEnabled ?? true,
createdAt: props.createdAt ?? new Date(),
sessionVersion: props.sessionVersion ?? 0,
demoExpiresAt: props.demoExpiresAt ?? null,
});
}
}
|