Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0021

Email and password self-registration, no verification, no reset

Trạng thái
accepted
Ngày
2026-09-27
Người quyết định
Alex

Context

Until now the only way into the system was the seeded admin: ADR-0007 built login, refresh and logout, and the technical plan recorded that "there is no path to create an account — every use case starts with Login". Every student story is blocked behind that. The plan also answers the question directly (§16): students may register themselves, STUDENT only, with no email verification, because the project has no mail provider and will not buy one.

Two constraints shape the rest. The seed script already refuses a SEED_ADMIN_PASSWORD under 12 characters, while loginSchema accepts 8 — so the repo carried two numbers with no stated rule. And Google sign-in, listed in ADR-0007 and the architecture notes as "phase 2", is dropped from the plan: the User.googleSub column stays, unused, rather than being removed and re-migrated later.

Decision

  • Self-registration is POST /auth/register, public, with email, name and password. It answers 201 with the same AuthSession login returns, so the new account is signed in without a second round trip.
  • STUDENT only. registerSchema has no role field, so Zod strips one from the body, and the service writes Role.STUDENT literally. Instructors are promoted by an admin; that is a different route, in a different module, behind @Roles().
  • emailVerifiedAt is stamped at registration. There is no mail provider, so a "pending verification" state would be a state nothing can ever leave. The column stays, and means "this address was accepted at this time" — the day a provider exists, only registration changes.
  • Password minimum 12 on anything we set (PASSWORD_MIN_LENGTH in packages/shared), on both registration and password change, matching the rule the seed script has always enforced. Login keeps its ≥8 check: it is not a policy but a "you clearly did not type a password" filter, and raising it would lock out any account created under the old rule. The maximum is 72 because bcrypt reads no further, and silently truncating would make two different passwords the same one.
  • No password reset flow. A reset is an email, and there is no email. A user who forgets a password asks an admin, who will get an admin-side reset route when the admin module lands.
  • Duplicate email is decided by the unique index, not by a findFirst before the insert. The password is hashed first either way, so a taken address and a free one cost the same time — a pre-check answers "taken" without paying for bcrypt, which turns the sign-up form into an account-enumeration stopwatch. The answer is 409 with code: "EMAIL_TAKEN".
  • A code on the error envelope. apiErrorSchema gains an optional code from ERROR_CODES in packages/shared, set by throwing new ConflictException({ code, message }). It is for the failures a client must act on differently — a taken email highlights a field, an unknown major reloads a list. message stays human and may be reworded freely.
  • POST /auth/register is throttled to 10 per hour per client, not per minute: registration abuse is bulk account creation, and a whole campus can share one NAT address.
  • /users/me is a new module: GET returns the full profile, PATCH writes only name, avatarUrl, majorId, semesterId, timezone, and POST /users/me/password changes the password and then revokes every refresh token of that account. majorId and semesterId must name a row that exists and is isActive; otherwise 422 with a code.
  • Refresh-token reuse detection is unchanged (ADR-0007). replacedAt is now written when a token is spent by rotation, so a revoked row can be told apart from one the reuse sweep killed. It is observability only: nothing reads it to decide access, and there is no grace window — a replayed token still revokes the whole chain.
  • Google OAuth is dropped. User.googleSub is left in place, unused and unique.

Alternatives considered

  • Verification email with a pending state. Rejected: no mail provider, and the plan says one will not be bought. A state with no exit is worse than no state.
  • Raising the login minimum to 12 as well. Rejected: it locks out every account created under the old rule, including the one an examiner might be holding, and it buys nothing — the policy belongs where a password is written.
  • Self-service password reset with a token emailed out. Rejected for the same reason as verification. An admin-side reset covers the real case.
  • Letting role through and validating it. Rejected: the safest field is the one the schema does not have.
  • A short reuse grace window keyed on replacedAt (accept a token replaced in the last few seconds, for flaky mobile networks). Rejected: it trades the one property that makes rotation worth having — a replayed token is proof of a leak — for a retry the client can do itself.

Consequences

  • Every student story can now start from an empty database: register, sign in, use the product.
  • Nothing proves an address belongs to the person who typed it. A stolen address cannot be recovered and a typo cannot be corrected by the user — both need an admin. Accepted, recorded here so it is not discovered during the defence.
  • A forgotten password is an admin ticket. The admin module owes a reset route.
  • Two password minimums live in the codebase on purpose. PASSWORD_MIN_LENGTH is the one to use; the 8 in loginSchema is legacy tolerance and is commented as such.
  • The error envelope has one more optional field. ApiErrorDoc carries it, and the OpenAPI drift guard in test/openapi.e2e-spec.ts keeps the two in step.
  • AuditLog is written for the first time: registration, profile update and password change, with action taken from AUDIT_ACTIONS in packages/shared. A failed audit write is logged, never raised — the account really was created, and a 500 would say otherwise.
  • A password change signs the user out everywhere, including the tab they changed it in. The web client has to handle that; it is the behaviour users expect from a password change.