Skip to content

fix(auth): create missing UserOptions for OAuth users - #1012

Merged
sinamics merged 5 commits into
sinamics:mainfrom
maaad:fix/oauth-useroptions
Sep 13, 2026
Merged

sinamics merged 5 commits into
sinamics:mainfrom
maaad:fix/oauth-useroptions

Conversation

@maaad

@maaad maaad commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Problem

Users created through OAuth / Better Auth do not get a corresponding UserOptions record.
The regular ZTNet registration flow creates UserOptions together with the user, but the Better Auth user creation flow only creates the User record.
As a result, auth.me may receive:

user.options === null

and then fail on:

user.options.localControllerUrlPlaceholder = ...

with:

Cannot set properties of null (setting 'localControllerUrlPlaceholder')

This breaks the user settings/profile UI for OAuth-created users even though the OAuth session itself contains the correct user data.

Fix

This PR addresses both new and existing OAuth users:

  • add a databaseHooks.user.create.after hook to create UserOptions after Better Auth creates a user;
  • use upsert so the hook is idempotent;
  • add a fallback in auth.me that creates missing UserOptions for users created before this fix;
  • keep the default localControllerUrl behavior consistent with the regular registration flow.

Result

New OAuth users now get a valid UserOptions record during creation.
Existing OAuth users with missing UserOptions are automatically repaired on the next auth.me request.
No database migration is required because the UserOptions model and userId relation already exist.

Testing

Tested with an OAuth user created through Microsoft Entra ID.
Before the fix:

GET /api/trpc/auth.me -> 500
Cannot set properties of null (setting 'localControllerUrlPlaceholder')

After the fix:

  • OAuth login succeeds;
  • UserOptions is created;
  • auth.me returns successfully;
  • user settings correctly display name, email and role.

@github-actions github-actions Bot added the ztnet Main Application label Sep 12, 2026
@sinamics

Copy link
Copy Markdown
Owner

Thx for this pr. Can you fix the lint issue. You can probably just run npm run lint:fix
or if you are working in devcontainer, just save the file and it will autofix the lint.

@sinamics

Copy link
Copy Markdown
Owner

⭐

@sinamics
sinamics merged commit c59d038 into sinamics:main Sep 13, 2026
5 checks passed
@maaad
maaad deleted the fix/oauth-useroptions branch September 13, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ztnet Main Application

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants