getSession({ headers})) fails if JWT is in Bearer even with valid cookie #423

Closed
opened 2026-03-13 07:45:17 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @daveycodez on GitHub (Dec 18, 2024).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Use the JWT plugin and pass the "Bearer JWT" Authorization header.

This will cause getSession to fail, I assume because it doesn't support JWT bearers. I'm using Bearer JWT for my Neon Postgres Proxy API, but I also need to use getSession in that proxy. My current hack is to check cookies for auth-token and omit the authorization header if it is present. However, for my Capacitor app I pass the normal Bearer token (not JWT) for all requests.

IMO the ideal solution would be to authorize JWT's in getSession, but a second best fix would be to attempt to authenticate based on the Authorization Bearer header, and if that fails fallback to the cookie

Current vs. Expected behavior

getSession should still work if there is a valid cookie but an invalid authorization header.

What version of Better Auth are you using?

1.2.2

Provide environment information

Next.js 15 Pages Router
Capacitor

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
import { jwt, bearer } from "better-auth/plugins"
import { NeonHTTPDialect } from "kysely-neon"

export const auth = betterAuth({
    database: {
        dialect: new NeonHTTPDialect({
            connectionString: process.env.DATABASE_URL!,
        }),
        type: "postgres"
    },
    emailAndPassword: { enabled: true },
    plugins: [
        jwt({
            jwks: {
                keyPairConfig: {
                    alg: "ES256"
                }
            }
        }),
        bearer()
    ],
    trustedOrigins: [
        "http://localhost:3000",
    ]
})

Additional context

No response

Originally created by @daveycodez on GitHub (Dec 18, 2024). ### Is this suited for github? - [X] Yes, this is suited for github ### To Reproduce Use the JWT plugin and pass the "Bearer JWT" Authorization header. This will cause getSession to fail, I assume because it doesn't support JWT bearers. I'm using Bearer JWT for my Neon Postgres Proxy API, but I also need to use getSession in that proxy. My current hack is to check cookies for auth-token and omit the authorization header if it is present. However, for my Capacitor app I pass the normal Bearer token (not JWT) for all requests. IMO the ideal solution would be to authorize JWT's in getSession, but a second best fix would be to attempt to authenticate based on the Authorization Bearer header, and if that fails fallback to the cookie ### Current vs. Expected behavior getSession should still work if there is a valid cookie but an invalid authorization header. ### What version of Better Auth are you using? 1.2.2 ### Provide environment information ```bash Next.js 15 Pages Router Capacitor ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" import { jwt, bearer } from "better-auth/plugins" import { NeonHTTPDialect } from "kysely-neon" export const auth = betterAuth({ database: { dialect: new NeonHTTPDialect({ connectionString: process.env.DATABASE_URL!, }), type: "postgres" }, emailAndPassword: { enabled: true }, plugins: [ jwt({ jwks: { keyPairConfig: { alg: "ES256" } } }), bearer() ], trustedOrigins: [ "http://localhost:3000", ] }) ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 07:45:17 -05:00
Author
Owner

@daveycodez commented on GitHub (Dec 18, 2024):

This is my hack atm

 const headers = new Headers()

    request.headers.entries().forEach(([key, value]) => {
        if (key == "authorization" && request.headers.get("cookie")?.includes("auth-token")) return
        headers.set(key, value)
    })

    const session = await auth.api.getSession({ headers })
@daveycodez commented on GitHub (Dec 18, 2024): This is my hack atm ```ts const headers = new Headers() request.headers.entries().forEach(([key, value]) => { if (key == "authorization" && request.headers.get("cookie")?.includes("auth-token")) return headers.set(key, value) }) const session = await auth.api.getSession({ headers }) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#423