Skip to content

Fix signup email, user updates and code login - #304

Open
kaareal wants to merge 6 commits into
masterfrom
fix/api-bugs
Open

kaareal wants to merge 6 commits into
masterfrom
fix/api-bugs

Conversation

@kaareal

@kaareal kaareal commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Three bugs found while testing the auth flows in the UI. Each one is reproducible on master today. Every fix has a test that fails without it.

Bug Cause Fix
Password signup returns 500 renderTemplate passed a body loaded from the database to the renderer as a filename. With dir set the renderer resolves it as a path, and anything longer than the filesystem name limit throws ENAMETOOLONG. The welcome template is a database template, so every password signup hit it. The account was created first, so it looked half-broken. Pass dir only when the template really is a file.
Saving a user reports "A user with that email already exists" Update validation excludes the document from unique checks by an id in the body, and no client sends one. Any save that included the unchanged email failed, which blocked role editing in the admin UI. The user routes supply the id from the fetched document.
AUTH_TYPE=code login fails with "Unknown field password" The login screen posted the whole form, including password, and sent the channel as authChannel. Send email, type and channel.

Why the tests didn't catch these

The template test used a short body, which stays under the name limit and falls back to the literal string. The signup test passes because no database template exists in the test database, so the file is used. The new test uses a long multi-line body like the real templates.

Verified in the browser

Signup, admin user edit, role assignment and the full code-login flow (request, emailed code, confirm), plus the API and web suites.

- Templates stored in the database were passed to the renderer as a
  filename, so any body longer than the filesystem name limit threw
  ENAMETOOLONG. Password signup failed this way on the welcome email.
- Update validation excludes a document from unique checks by an id in
  the body, which clients don't send, so saving a user with an unchanged
  email reported the address as taken. The user routes now supply it.
- The login screen sent "password" and "authChannel" to /1/auth/otp/send,
  which rejects both, breaking AUTH_TYPE=code.
@github-actions

Copy link
Copy Markdown

API Changes

No changes.

The softUnique validator excludes a document by an id in the request body,
which clients don't send on updates, so a user collided with their own row
when saving an unchanged email or phone.

This was handled by a middleware wired into the two user update routes by
hand. Move it to the middleware that already resolves the target document
(fetchByParam, fetchByParamWithSlug, isSelf) so every update route gets it
without opting in, and any model that later gains a unique field is covered.
Comment on lines +44 to +52
// Unique checks in update validation exclude the target document by an id in
// the body, which clients have no reason to send. Take it from the document the
// route already resolved. Update validation strips it again before assign.
function excludeFromUniqueChecks(ctx, doc) {
if (ctx.method === 'PATCH' || ctx.method === 'PUT') {
ctx.request.body.id = doc.id;
}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewplummer can you take a look to see if this best solution

The problem is that right, if you try to patch users/me

Image

You get this, this because the patch doesnt set the id in the body.

if there is no id in the body the uniqueness check triggers and fails

// Unique checks in update validation exclude the target document by an id in
// the body, which clients have no reason to send. Take it from the document the
// route already resolved. Update validation strips it again before assign.
function excludeFromUniqueChecks(ctx, doc) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I understand this

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copy is bad ?

Or you dont understand the problem ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants