[GH-ISSUE #2517] useSession should return JWT #17867

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

Originally created by @widavies on GitHub (May 2, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2517

Is this suited for github?

  • Yes, this is suited for github

As stated in the docs, there are two ways to retrieve the JWT when using the JWT plugin:

  1. Make a fetch request to /api/auth/token
  2. Call authClient.getSession()

As I understand it, useSession is a reactive wrapper around method #2 (authClient.getSession()). The network request in Chrome DevTools shows that the set-auth-jwt header is already present in the network response placed by useSession. However - there's no way to get at it. It would be really convenient to have a way to get at the JWT - this saves an extra network request, which is a big deal for things like Convex where the JWT is used for accessing the database.

Hopefully this change isn't too difficult - the response header is there already, we just need a way to get at it.

Describe the solution you'd like

Modify useSession:

// Consistent with the authClient.getSession() example
useSession({
  fetchOptions: {
      onSuccess: (ctx)=>{
        const jwt = ctx.response.headers.get("set-auth-jwt")
      }
    }
})

Or alternatively, as a prop:

const { jwt } = useSession();

Describe alternatives you've considered

Right now, placing a second network request is fine, but this would save extra network requests and would be a lot easier to use.

Additional context

One potential problem with this is session cookie caching having a different TTL than JWTs. Ideally the useSession hook would automatically re-run to refresh the session or the JWT when needed. For most cases, the JWT will have a shorter TTL than the session, so the JWT can be cached within the session cookie.

This would make the JWT plugin way more convenient and easy to use because it saves developers the time in implementing a refresh system for JWT renewal. Making the useSession hook auto re-run to keep the session & JWT up to date would be phenomenal.

Originally created by @widavies on GitHub (May 2, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2517 ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. As [stated in the docs](https://www.better-auth.com/docs/plugins/jwt#retrieve-the-token), there are two ways to retrieve the JWT when using the JWT plugin: 1) Make a fetch request to `/api/auth/token` 2) Call `authClient.getSession()` As I understand it, `useSession` is a reactive wrapper around method #2 (`authClient.getSession()`). The network request in Chrome DevTools shows that the `set-auth-jwt` header is already present in the network response placed by `useSession`. However - there's no way to get at it. It would be really convenient to have a way to get at the JWT - this saves an extra network request, which is a big deal for things like [Convex](https://www.convex.dev/) where the JWT is used for accessing the database. Hopefully this change isn't too difficult - the response header is there already, we just need a way to get at it. ### Describe the solution you'd like Modify `useSession`: ```ts // Consistent with the authClient.getSession() example useSession({ fetchOptions: { onSuccess: (ctx)=>{ const jwt = ctx.response.headers.get("set-auth-jwt") } } }) ``` Or alternatively, as a prop: ```ts const { jwt } = useSession(); ``` ### Describe alternatives you've considered Right now, placing a second network request is fine, but this would save extra network requests and would be a lot easier to use. ### Additional context One potential problem with this is session cookie caching having a different TTL than JWTs. Ideally the `useSession` hook would automatically re-run to refresh the session or the JWT when needed. For most cases, the JWT will have a shorter TTL than the session, so the JWT can be cached within the session cookie. This would make the JWT plugin way more convenient and easy to use because it saves developers the time in implementing a refresh system for JWT renewal. Making the `useSession` hook auto re-run to keep the session & JWT up to date would be phenomenal.
GiteaMirror added the locked label 2026-04-15 16:13:02 -05:00
Author
Owner

@PacifismPostMortem commented on GitHub (May 3, 2025):

Agreed, this would be great

<!-- gh-comment-id:2848730446 --> @PacifismPostMortem commented on GitHub (May 3, 2025): Agreed, this would be great
Author
Owner

@dosubot[bot] commented on GitHub (Aug 2, 2025):

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

Issue Summary:

  • You requested that the useSession hook be enhanced to return the JWT token directly to avoid extra network requests.
  • You also suggested that useSession should auto-refresh the JWT to handle differing TTLs between session cookies and JWTs.
  • A maintainer agreed this would be a valuable improvement.
  • The issue remains unresolved with no recent updates or progress.

Next Steps:

  • Please let me know if this feature is still relevant to the latest version of better-auth by commenting on this issue.
  • If I don’t hear back within 7 days, the issue will be automatically closed.

Thanks for your understanding and contribution!

<!-- gh-comment-id:3146583658 --> @dosubot[bot] commented on GitHub (Aug 2, 2025): Hi, @widavies. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You requested that the `useSession` hook be enhanced to return the JWT token directly to avoid extra network requests. - You also suggested that `useSession` should auto-refresh the JWT to handle differing TTLs between session cookies and JWTs. - A maintainer agreed this would be a valuable improvement. - The issue remains unresolved with no recent updates or progress. **Next Steps:** - Please let me know if this feature is still relevant to the latest version of better-auth by commenting on this issue. - If I don’t hear back within 7 days, the issue will be automatically closed. Thanks 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#17867