[GH-ISSUE #3559] Use of null breaking things #18276

Closed
opened 2026-04-15 16:42:02 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @Micnubinub on GitHub (Jul 23, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3559

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I think this commit here is braking things in my DB, and potentially others too:
https://github.com/better-auth/better-auth/commit/7d034604

export function setTokenUtil(
	token: string | null | undefined,
	ctx: AuthContext,
) {
	if (ctx.options.account?.encryptOAuthTokens && token) {
		return symmetricEncrypt({
			key: ctx.secret,
			data: token,
		});
	}
	return token ?? null;
}

the set token util that is now being useed everywhere is setting values that would have been set as undefined before to be null and null !== undefined, so as time goes on I think more people will start to get issues

Current vs. Expected behavior

expecting undefined but getting null. Might have to put it up as a breaking change

What version of Better Auth are you using?

1.3.3

Provide environment information

- MacOS 15.5

Which area(s) are affected? (Select all that apply)

Backend, Types

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @Micnubinub on GitHub (Jul 23, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3559 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce I think this commit here is braking things in my DB, and potentially others too: https://github.com/better-auth/better-auth/commit/7d034604 ```js export function setTokenUtil( token: string | null | undefined, ctx: AuthContext, ) { if (ctx.options.account?.encryptOAuthTokens && token) { return symmetricEncrypt({ key: ctx.secret, data: token, }); } return token ?? null; } ``` the set token util that is now being useed everywhere is setting values that would have been set as `undefined` before to be `null` and `null !== undefined`, so as time goes on I think more people will start to get issues ### Current vs. Expected behavior expecting `undefined` but getting `null`. Might have to put it up as a breaking change ### What version of Better Auth are you using? 1.3.3 ### Provide environment information ```bash - MacOS 15.5 ``` ### Which area(s) are affected? (Select all that apply) Backend, Types ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-15 16:42:02 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 23, 2025):

Hi @Micnubinub! I'm Dosu and I’m helping the better-auth team.

This is a valid concern: changing setTokenUtil to return null instead of undefined is a breaking change, especially for downstream code and database adapters that distinguish between these values. Many ORMs and serializers treat null and undefined differently, which can lead to subtle bugs or unexpected persistence behavior if your schema or logic expects undefined for missing values.

There’s currently no mention of this change in the release notes or migration docs, and the pull request that introduced it doesn’t flag it as breaking. Since setTokenUtil is used throughout the codebase, this change could impact any place where token values are stored, persisted, or checked for presence, especially in plugins and adapters that rely on strict type contracts or default values (source).

Until this is officially addressed, you can mitigate issues by explicitly checking for both null and undefined in your code, or by normalizing values before passing them to your database layer (e.g., converting null back to undefined if your logic or schema expects it). If you maintain custom adapters or plugins, review their handling of token fields to ensure compatibility.

This change should be documented as a breaking change in future releases, especially for users relying on strict type or database semantics.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

<!-- gh-comment-id:3106973709 --> @dosubot[bot] commented on GitHub (Jul 23, 2025): <!-- Greeting --> Hi @Micnubinub! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a valid concern: changing setTokenUtil to return null instead of undefined is a breaking change, especially for downstream code and database adapters that distinguish between these values. Many ORMs and serializers treat null and undefined differently, which can lead to subtle bugs or unexpected persistence behavior if your schema or logic expects undefined for missing values. There’s currently no mention of this change in the release notes or migration docs, and the pull request that introduced it doesn’t flag it as breaking. Since setTokenUtil is used throughout the codebase, this change could impact any place where token values are stored, persisted, or checked for presence, especially in plugins and adapters that rely on strict type contracts or default values ([source](https://github.com/better-auth/better-auth/pull/852)). Until this is officially addressed, you can mitigate issues by explicitly checking for both null and undefined in your code, or by normalizing values before passing them to your database layer (e.g., converting null back to undefined if your logic or schema expects it). If you maintain custom adapters or plugins, review their handling of token fields to ensure compatibility. This change should be documented as a breaking change in future releases, especially for users relying on strict type or database semantics. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/5e1ee051-4b26-4f8c-b69f-05f3a280b5dd?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/5e1ee051-4b26-4f8c-b69f-05f3a280b5dd?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/5e1ee051-4b26-4f8c-b69f-05f3a280b5dd?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/5e1ee051-4b26-4f8c-b69f-05f3a280b5dd?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/5e1ee051-4b26-4f8c-b69f-05f3a280b5dd?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/5e1ee051-4b26-4f8c-b69f-05f3a280b5dd?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/5e1ee051-4b26-4f8c-b69f-05f3a280b5dd?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3559)
Author
Owner

@Micnubinub commented on GitHub (Jul 23, 2025):

(SurrealDB users) I fixed my DB by changingoption<string> to option<string | NULL>, other DBs can do something similar

DEFINE FIELD OVERWRITE ipAddress ON session TYPE option<string | NULL>;
DEFINE FIELD OVERWRITE location ON session TYPE option<string | NULL>;
DEFINE FIELD OVERWRITE userAgent ON session TYPE option<string | NULL>;
DEFINE FIELD OVERWRITE idToken ON account TYPE option<string | NULL>;
DEFINE FIELD OVERWRITE accessToken ON account TYPE option<string | NULL>;
DEFINE FIELD OVERWRITE refreshToken ON account TYPE option<string | NULL>;
DEFINE FIELD OVERWRITE accessTokenExpiresAt ON account TYPE option<datetime | NULL>;
DEFINE FIELD OVERWRITE refreshTokenExpiresAt ON account TYPE option<datetime | NULL>;
DEFINE FIELD OVERWRITE name ON passkey TYPE option<string | NULL>;
<!-- gh-comment-id:3107035524 --> @Micnubinub commented on GitHub (Jul 23, 2025): (SurrealDB users) I fixed my DB by changing`option<string>` to `option<string | NULL>`, other DBs can do something similar ```sql DEFINE FIELD OVERWRITE ipAddress ON session TYPE option<string | NULL>; DEFINE FIELD OVERWRITE location ON session TYPE option<string | NULL>; DEFINE FIELD OVERWRITE userAgent ON session TYPE option<string | NULL>; DEFINE FIELD OVERWRITE idToken ON account TYPE option<string | NULL>; DEFINE FIELD OVERWRITE accessToken ON account TYPE option<string | NULL>; DEFINE FIELD OVERWRITE refreshToken ON account TYPE option<string | NULL>; DEFINE FIELD OVERWRITE accessTokenExpiresAt ON account TYPE option<datetime | NULL>; DEFINE FIELD OVERWRITE refreshTokenExpiresAt ON account TYPE option<datetime | NULL>; DEFINE FIELD OVERWRITE name ON passkey TYPE option<string | NULL>; ```
Author
Owner

@Bekacru commented on GitHub (Jul 24, 2025):

Generally, an optional field is treated as nullable by the library, so setting it to null would be expected to work.

The issue is more on how Surreal DB adapter is handling nullable values, undefined is used by our first‑party adapters to indicate that the existing data should remain unchanged not to set an actual value since many databases don’t support undefined.

That said, the PR should revert this change

<!-- gh-comment-id:3111944532 --> @Bekacru commented on GitHub (Jul 24, 2025): Generally, an optional field is treated as nullable by the library, so setting it to null would be expected to work. The issue is more on how Surreal DB adapter is handling nullable values, undefined is used by our first‑party adapters to indicate that the existing data should remain unchanged not to set an actual value since many databases don’t support undefined. That said, the PR should revert this change
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#18276