SERVER_ERROR: SyntaxError: Unterminated string in JSON at position 191 at JSON.parse (<anonymous>) at getJwtToken #1113

Closed
opened 2026-03-13 08:23:09 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @neerajkumarSenior on GitHub (Apr 25, 2025).

Is this suited for github?

Example using jose with remote JWKS

import { jwtVerify, createRemoteJWKSet } from 'jose'

async function validateToken(token: string) {
try {
const JWKS = createRemoteJWKSet(
new URL('http://localhost:5173/api/auth/jwks')
)
const { payload } = await jwtVerify(token, JWKS, {
issuer: 'http://localhost:5173', // Should match your JWT issuer, which is the BASE_URL
audience: 'http://localhost:5173', // Should match your JWT audience, which is the BASE_URL by default
})
return payload
} catch (error) {
console.error('Token validation failed:', error)
throw error
}
}

// Usage example
const token = 'your.jwt.token' // this is the token you get from the /api/auth/token endpoint
const payload = await validateToken(token)

  • Yes, this is suited for github

To Reproduce

when i add , Jwt , plugin , in project , when i login , throw the issue http://localhost:5173/api/auth/token , when i hit with , const jwt = ctx.response.headers.get("set-auth-jwt") this , return token , ,500 Internal Server Error

// docs
https://www.better-auth.com/docs/plugins/jwt

//
https://www.better-auth.com/docs/plugins/bearer

Image,

public key = {"crv":"Ed25519","x":"_yN7Ej34o8FFAaKXRRygXw2YSd-G8B4QkzegTv34GXY","kty":"OKP"}

private key = "d0fec33405e799807fd1a918f1882e965374e68fac773938eb60ad53ddd596a35a8050a92ed7ba069e3312efdf05f2cac9c22a55a081c1f7f6a58d65e182080f79bbf228af291390c8b5b0e82b364006e9c673da0d9bdf00bbcd643c52b8ea,

when i hit on postman http://localhost:5173/api/auth/token with ,
const jwt = ctx.response.headers.get("set-auth-jwt") // get token from it and pass for http://localhost:5173/api/auth/token , throw this 500
Internal Server Error

Current vs. Expected behavior

SERVER_ERROR: SyntaxError: Unterminated string in JSON at position 191
at JSON.parse ()
at getJwtToken (file:///D:/hu/mkf_web/node_modules/.pnpm/better-auth@1.2.5_typescript@5.8.3/node_modules/better-auth/dist/plugins/jwt/index.mjs:101:16)
at file:///D:/hu/mkf_web/node_modules/.pnpm/better-auth@1.2.5_typescript@5.8.3/node_modules/better-auth/dist/plugins/jwt/index.mjs:266:28
at Object.internalHandler [as handler] (file:///D:/hu/mkf_web/node_modules/.pnpm/better-call@1.0.9/node_modules/better-call/dist/index.js:527:22)
at runAfterHooks (file:///D:/hu/mkf_web/node_modules/.pnpm/better-auth@1.2.5_typescript@5.8.3/node_modules/better-auth/dist/api/index.mjs:543:22)
at api. (file:///D://mkf_web/node_modules/.pnpm/better-auth@1.2.5_typescript@5.8.3/node_modules/better-auth/dist/api/index.mjs:491:21)

What version of Better Auth are you using?

1.2.5 to 1.2.6

Provide environment information

windows 11

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

Client

Auth config (if applicable)

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


    plugins: [

        openAPI(),
        bearer(),
        jwt({
            jwt: {
              definePayload: ({user}) => {
                return {
                  id: user.id,
                  email: user.email,
                  role: user.role
                }
              }
            }
          }), 
         admin(), phoneNumber({
        sendOTP: ({ phoneNumber, code }, request) => {
            // Implement sending OTP code via SMS
        }
    }),
    



],
});

Additional context

No response

Originally created by @neerajkumarSenior on GitHub (Apr 25, 2025). ### Is this suited for github? [Example using jose with remote JWKS](https://www.better-auth.com/docs/plugins/jwt#example-using-jose-with-remote-jwks) import { jwtVerify, createRemoteJWKSet } from 'jose' async function validateToken(token: string) { try { const JWKS = createRemoteJWKSet( new URL('http://localhost:5173/api/auth/jwks') ) const { payload } = await jwtVerify(token, JWKS, { issuer: 'http://localhost:5173', // Should match your JWT issuer, which is the BASE_URL audience: 'http://localhost:5173', // Should match your JWT audience, which is the BASE_URL by default }) return payload } catch (error) { console.error('Token validation failed:', error) throw error } } // Usage example const token = 'your.jwt.token' // this is the token you get from the /api/auth/token endpoint const payload = await validateToken(token) - [x] Yes, this is suited for github ### To Reproduce when i add , Jwt , plugin , in project , when i login , throw the issue http://localhost:5173/api/auth/token , when i hit with , const jwt = ctx.response.headers.get("set-auth-jwt") this , return token , ,500 Internal Server Error // docs https://www.better-auth.com/docs/plugins/jwt // https://www.better-auth.com/docs/plugins/bearer ![Image](https://github.com/user-attachments/assets/c7c17df3-5852-47d1-b2b9-3d31888f727b), public key = {"crv":"Ed25519","x":"_yN7Ej34o8FFAaKXRRygXw2YSd-G8B4QkzegTv34GXY","kty":"OKP"} private key = "d0fec33405e799807fd1a918f1882e965374e68fac773938eb60ad53ddd596a35a8050a92ed7ba069e3312efdf05f2cac9c22a55a081c1f7f6a58d65e182080f79bbf228af291390c8b5b0e82b364006e9c673da0d9bdf00bbcd643c52b8ea, when i hit on postman http://localhost:5173/api/auth/token with , const jwt = ctx.response.headers.get("set-auth-jwt") // get token from it and pass for http://localhost:5173/api/auth/token , throw this 500 Internal Server Error ### Current vs. Expected behavior SERVER_ERROR: SyntaxError: Unterminated string in JSON at position 191 at JSON.parse (<anonymous>) at getJwtToken (file:///D:/hu/mkf_web/node_modules/.pnpm/better-auth@1.2.5_typescript@5.8.3/node_modules/better-auth/dist/plugins/jwt/index.mjs:101:16) at file:///D:/hu/mkf_web/node_modules/.pnpm/better-auth@1.2.5_typescript@5.8.3/node_modules/better-auth/dist/plugins/jwt/index.mjs:266:28 at Object.internalHandler [as handler] (file:///D:/hu/mkf_web/node_modules/.pnpm/better-call@1.0.9/node_modules/better-call/dist/index.js:527:22) at runAfterHooks (file:///D:/hu/mkf_web/node_modules/.pnpm/better-auth@1.2.5_typescript@5.8.3/node_modules/better-auth/dist/api/index.mjs:543:22) at api.<computed> (file:///D://mkf_web/node_modules/.pnpm/better-auth@1.2.5_typescript@5.8.3/node_modules/better-auth/dist/api/index.mjs:491:21) ### What version of Better Auth are you using? 1.2.5 to 1.2.6 ### Provide environment information ```bash windows 11 ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, plugins: [ openAPI(), bearer(), jwt({ jwt: { definePayload: ({user}) => { return { id: user.id, email: user.email, role: user.role } } } }), admin(), phoneNumber({ sendOTP: ({ phoneNumber, code }, request) => { // Implement sending OTP code via SMS } }), ], }); ``` ### Additional context _No response_
Author
Owner

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

Hi, @neerajkumarSenior. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • A SyntaxError occurs due to an unterminated string in JSON when parsing a JWT token.
  • This error results in a 500 Internal Server Error during login.
  • You provided code snippets and documentation links to reproduce the issue.
  • No comments or developments have been made on this issue yet.

Next Steps:

  • Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting here.
  • If there is no response, the issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Jul 25, 2025): Hi, @neerajkumarSenior. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - A `SyntaxError` occurs due to an unterminated string in JSON when parsing a JWT token. - This error results in a 500 Internal Server Error during login. - You provided code snippets and documentation links to reproduce the issue. - No comments or developments have been made on this issue yet. **Next Steps:** - Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting here. - If there is no response, the issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1113