"[object Object]" is not valid JSON when creating an OAuth client with metadata #2654

Closed
opened 2026-03-13 10:11:08 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @bauerj on GitHub (Jan 5, 2026).

Originally assigned to: @Paola3stefania on GitHub.

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Enable OAuth 2.1 provider plugin.
  2. Create a new client using admin endpoint:
const client = await auth.api.adminCreateOAuthClient({
      headers: {
        cookie: cookie ?? '',
      },
      body: {
        redirect_uris: [`https://example.com/auth/callback`],
        skip_consent: true,
        metadata: {
          a: 'b',
        },
      },
    });

Current vs. Expected behavior

I expected this to work since metadata is typed as Record<string, unkown>. However, I get this exception:

SyntaxError: "[object Object]" is not valid JSON
    at JSON.parse (<anonymous>)
    at schemaToOAuth (file:///home/bauerj/project/api/node_modules/.pnpm/@better-auth+oauth-provider@1.4.10_@better-auth+core@1.4.10_@better-auth+utils@0.3.0_@b_56bb7b0be66b3a227cf5ae860359839b/node_modules/@better-auth/oauth-provider/dist/index.mjs:1468:22)
    at createOAuthClientEndpoint (file:///home/bauerj/project/api/node_modules/.pnpm/@better-auth+oauth-provider@1.4.10_@better-auth+core@1.4.10_@better-auth+utils@0.3.0_@b_56bb7b0be66b3a227cf5ae860359839b/node_modules/@better-auth/oauth-provider/dist/index.mjs:1405:18)
    at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
    at async internalHandler (/home/bauerj/project/api/node_modules/.pnpm/better-call@1.1.7_zod@4.2.1/node_modules/better-call/src/endpoint.ts:507:20)
    at async <anonymous> (/home/bauerj/project/api/node_modules/.pnpm/better-auth@1.4.10_drizzle-kit@0.31.7_drizzle-orm@0.45.1_@types+pg@8.16.0_kysely@0.28.9_e69517855bdf5a28be3e1242deda0275/node_modules/better-auth/src/api/to-auth-endpoints.ts:119:22)
    at async AuthService.createOAuthClient (/home/bauerj/project/api/src/auth/auth.service.ts:21:20)

Apparently, it's trying to parse an object as JSON.

What version of Better Auth are you using?

1.4.10

System info

{
  "system": {
    "platform": "linux",
    "arch": "x64",
    "version": "#1 SMP PREEMPT_DYNAMIC Sun Jun 29 08:14:24 UTC 2025",
    "release": "6.15.4-locietta-WSL2-xanmod1",
    "cpuCount": 8,
    "cpuModel": "11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz",
    "totalMemory": "15.47 GB",
    "freeMemory": "9.23 GB"
  },
  "node": {
    "version": "v24.11.1",
    "env": "local"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.6.2"
  },
  "frameworks": null,
  "databases": [
    {
      "name": "pg",
      "version": "8.16.3"
    },
    {
      "name": "drizzle",
      "version": "0.45.1"
    }
  ],
  "betterAuth": {
    "version": "1.4.10",
    "config": "REDACTED"
    }
  }
}

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

Backend, Package

Auth config (if applicable)

const auth = betterAuth({
  disabledPaths: [
    "/token",
  ],
  plugins: [
    jwt(),
    oauthProvider({
    allowDynamicClientRegistration: false,
    allowUnauthenticatedClientRegistration: false,
    disableJwtPlugin: true,
    loginPage: '/login',
    consentPage: '/consent',
  })
  ],
});

Additional context

When removing the JSON.parse from https://github.com/better-auth/better-auth/blob/0827fc3f49c4968ea37f4a42a26a309edf75bea1/packages/oauth-provider/src/register.ts#L357 it works just fine.

Originally created by @bauerj on GitHub (Jan 5, 2026). Originally assigned to: @Paola3stefania on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Enable OAuth 2.1 provider plugin. 2. Create a new client using admin endpoint: ``` const client = await auth.api.adminCreateOAuthClient({ headers: { cookie: cookie ?? '', }, body: { redirect_uris: [`https://example.com/auth/callback`], skip_consent: true, metadata: { a: 'b', }, }, }); ``` ### Current vs. Expected behavior I expected this to work since metadata is typed as Record<string, unkown>. However, I get this exception: ``` SyntaxError: "[object Object]" is not valid JSON at JSON.parse (<anonymous>) at schemaToOAuth (file:///home/bauerj/project/api/node_modules/.pnpm/@better-auth+oauth-provider@1.4.10_@better-auth+core@1.4.10_@better-auth+utils@0.3.0_@b_56bb7b0be66b3a227cf5ae860359839b/node_modules/@better-auth/oauth-provider/dist/index.mjs:1468:22) at createOAuthClientEndpoint (file:///home/bauerj/project/api/node_modules/.pnpm/@better-auth+oauth-provider@1.4.10_@better-auth+core@1.4.10_@better-auth+utils@0.3.0_@b_56bb7b0be66b3a227cf5ae860359839b/node_modules/@better-auth/oauth-provider/dist/index.mjs:1405:18) at process.processTicksAndRejections (node:internal/process/task_queues:103:5) at async internalHandler (/home/bauerj/project/api/node_modules/.pnpm/better-call@1.1.7_zod@4.2.1/node_modules/better-call/src/endpoint.ts:507:20) at async <anonymous> (/home/bauerj/project/api/node_modules/.pnpm/better-auth@1.4.10_drizzle-kit@0.31.7_drizzle-orm@0.45.1_@types+pg@8.16.0_kysely@0.28.9_e69517855bdf5a28be3e1242deda0275/node_modules/better-auth/src/api/to-auth-endpoints.ts:119:22) at async AuthService.createOAuthClient (/home/bauerj/project/api/src/auth/auth.service.ts:21:20) ``` Apparently, it's trying to parse an object as JSON. ### What version of Better Auth are you using? 1.4.10 ### System info ```bash { "system": { "platform": "linux", "arch": "x64", "version": "#1 SMP PREEMPT_DYNAMIC Sun Jun 29 08:14:24 UTC 2025", "release": "6.15.4-locietta-WSL2-xanmod1", "cpuCount": 8, "cpuModel": "11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz", "totalMemory": "15.47 GB", "freeMemory": "9.23 GB" }, "node": { "version": "v24.11.1", "env": "local" }, "packageManager": { "name": "npm", "version": "11.6.2" }, "frameworks": null, "databases": [ { "name": "pg", "version": "8.16.3" }, { "name": "drizzle", "version": "0.45.1" } ], "betterAuth": { "version": "1.4.10", "config": "REDACTED" } } } ``` ### Which area(s) are affected? (Select all that apply) Backend, Package ### Auth config (if applicable) ```typescript const auth = betterAuth({ disabledPaths: [ "/token", ], plugins: [ jwt(), oauthProvider({ allowDynamicClientRegistration: false, allowUnauthenticatedClientRegistration: false, disableJwtPlugin: true, loginPage: '/login', consentPage: '/consent', }) ], }); ``` ### Additional context When removing the `JSON.parse` from https://github.com/better-auth/better-auth/blob/0827fc3f49c4968ea37f4a42a26a309edf75bea1/packages/oauth-provider/src/register.ts#L357 it works just fine.
GiteaMirror added the plugin label 2026-03-13 10:11:08 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 5, 2026):

It seems that you're encountering a SyntaxError due to attempting to parse an object as JSON when creating an OAuth client with metadata. Based on your description, you're using Better Auth v1.4.10, and the issue arises when the metadata property is being parsed improperly.

From the issues logged, there are reports of similar errors involving objects incorrectly treated as strings when forming JSON responses. For instance, in another case, a setup was failing due to improper handling of object types in the JSON payloads, resulting in the same '[object Object]' parsing error.

As a potential step to resolve this, ensure that the metadata being sent is transformed into a proper JSON-compatible format (e.g., using JSON.stringify() if needed in your code). Another approach is to review if there's a safe parsing function for attributes like metadata to avoid any implicit conversions that could lead to issues during JSON serialization.

If these steps do not resolve the problem, I recommend checking the Better Auth documentation regarding JSON handling or the related GitHub issues. Additionally, feel free to tag @bekacru and @ping__ for targeted assistance from the Better Auth team.

Have you tried logging the metadata right before you send it to see its structure? That could provide more insights into what's being sent.

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Jan 5, 2026): It seems that you're encountering a `SyntaxError` due to attempting to parse an object as JSON when creating an OAuth client with metadata. Based on your description, you're using Better Auth v1.4.10, and the issue arises when the `metadata` property is being parsed improperly. From the issues logged, there are reports of similar errors involving objects incorrectly treated as strings when forming JSON responses. For instance, in another case, a setup was failing due to improper handling of object types in the JSON payloads, resulting in the same '[object Object]' parsing error. As a potential step to resolve this, ensure that the `metadata` being sent is transformed into a proper JSON-compatible format (e.g., using `JSON.stringify()` if needed in your code). Another approach is to review if there's a safe parsing function for attributes like `metadata` to avoid any implicit conversions that could lead to issues during JSON serialization. If these steps do not resolve the problem, I recommend checking the Better Auth documentation regarding JSON handling or the related GitHub issues. Additionally, feel free to tag @bekacru and @ping__ for targeted assistance from the Better Auth team. Have you tried logging the `metadata` right before you send it to see its structure? That could provide more insights into what's being sent. _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@bauerj commented on GitHub (Jan 5, 2026):

@better-auth-agent Better Auth's auth.api.adminCreateOAuthClient method types body.metadata as a Record. As you can see in the code above, the metadata is just passed as {a: 'b'} to the method. If there is an issue in sending the data, surely it must be in the adminCreateOAuthClient method?

@bauerj commented on GitHub (Jan 5, 2026): @better-auth-agent Better Auth's `auth.api.adminCreateOAuthClient` method types body.metadata as a Record. As you can see in the code above, the metadata is just passed as {a: 'b'} to the method. If there is an issue in sending the data, surely it must be in the adminCreateOAuthClient method?
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 5, 2026):

It seems like you're encountering a SyntaxError due to the metadata being parsed incorrectly when creating an OAuth client. The metadata field is expected to be a valid JSON object, which may not be reaching the API in the proper format.

To troubleshoot this issue, ensure that the object being passed in the metadata field is correctly formatted as JSON before sending it. You might want to log the output of the metadata just before the adminCreateOAuthClient method to ensure it's what you expect.

Also, keep in mind that removing JSON.parse from the library, as you've mentioned, is a workaround but not a permanent solution. Consider checking if there's an update or issue in the repository regarding this behavior that could address it better.

If the issue persists, please tag @bekacru or @ping__ for more assistance from the Better Auth team.

What further information would you need about implementing authentication with Better Auth?

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Jan 5, 2026): It seems like you're encountering a `SyntaxError` due to the `metadata` being parsed incorrectly when creating an OAuth client. The `metadata` field is expected to be a valid JSON object, which may not be reaching the API in the proper format. To troubleshoot this issue, ensure that the object being passed in the `metadata` field is correctly formatted as JSON before sending it. You might want to log the output of the `metadata` just before the `adminCreateOAuthClient` method to ensure it's what you expect. Also, keep in mind that removing `JSON.parse` from the library, as you've mentioned, is a workaround but not a permanent solution. Consider checking if there's an update or issue in the repository regarding this behavior that could address it better. If the issue persists, please tag @bekacru or @ping__ for more assistance from the Better Auth team. What further information would you need about implementing authentication with Better Auth? <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@bauerj commented on GitHub (Jan 5, 2026):

@better-auth-agent I'm afraid this discussion is pointless. I have provided all information you asked for. Do I have to explain the issue to you or will a human look at this eventually?

@bauerj commented on GitHub (Jan 5, 2026): @better-auth-agent I'm afraid this discussion is pointless. I have provided all information you asked for. Do I have to explain the issue to you or will a human look at this eventually?
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 5, 2026):

It looks like you're encountering an error where you're getting the message SyntaxError: "[object Object]" is not valid JSON while trying to create an OAuth client with metadata. This typically indicates that the system is trying to parse an object into JSON incorrectly.

Based on some previous reports, similar issues have been observed when the metadata field is treated improperly. For instance, an error was logged stating that non-safe JSON parsing leads to problems when an object is passed as a string. The expectation is that the metadata is typed correctly, but it may have been handled incorrectly in the parsing logic [1][2][3].

To troubleshoot this issue, you could try the following steps:

  1. Verify that the values you're providing in the metadata object are indeed serializable to JSON. Ensure that no circular references or unsupported types are present.
  2. Check if upgrading to a later or different version of Better Auth resolves this issue, as updates may have fixed bugs related to JSON parsing.
  3. Consider altering the way metadata is processed or how data types are sent to the API endpoint.

If you're still facing issues, it might be helpful to tag a member of the Better Auth team, like @bekacru or @ping__, for more specialized assistance.

What other issues related to authentication are you currently encountering in your project? I'd be happy to help!

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Jan 5, 2026): It looks like you're encountering an error where you're getting the message `SyntaxError: "[object Object]" is not valid JSON` while trying to create an OAuth client with metadata. This typically indicates that the system is trying to parse an object into JSON incorrectly. Based on some previous reports, similar issues have been observed when the metadata field is treated improperly. For instance, an error was logged stating that non-safe JSON parsing leads to problems when an object is passed as a string. The expectation is that the metadata is typed correctly, but it may have been handled incorrectly in the parsing logic [[1]](https://github.com/better-auth/better-auth/issues/4862)[[2]](https://github.com/better-auth/better-auth/issues/2442)[[3]](https://github.com/better-auth/better-auth/pull/1359). To troubleshoot this issue, you could try the following steps: 1. Verify that the values you're providing in the `metadata` object are indeed serializable to JSON. Ensure that no circular references or unsupported types are present. 2. Check if upgrading to a later or different version of Better Auth resolves this issue, as updates may have fixed bugs related to JSON parsing. 3. Consider altering the way metadata is processed or how data types are sent to the API endpoint. If you're still facing issues, it might be helpful to tag a member of the Better Auth team, like @bekacru or @ping__, for more specialized assistance. What other issues related to authentication are you currently encountering in your project? I'd be happy to help! <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@Paola3stefania commented on GitHub (Jan 9, 2026):

@bauerj looking into this

@Paola3stefania commented on GitHub (Jan 9, 2026): @bauerj looking into this
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2654