How to verify the request on the server-side? #783

Closed
opened 2026-03-13 08:04:08 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @thinh-trinh-duc on GitHub (Mar 5, 2025).

Is this suited for github?

  • Yes, this is suited for github

Hi team,
Thanks for the great project, I've been using better-auth and I love it.
I am now using better-auth with react-router@v7 and my use case is to verify the request if it is authenticated or not on the server side.
Is there any API that I can just pass in the request object and better-auth will tell if it is valid or not?

NOTE: If better-auth doesn't have that before, I'm willing to contribute to it as I think this is a totally valid use case.

Thank you

Describe the solution you'd like

Something like this:

const loader = async ({ request } : Route.LoaderArgs) => {
  const isAuthenticated = auth.verify(request)
  ...
}

Describe alternatives you've considered

No

Additional context

No response

Originally created by @thinh-trinh-duc on GitHub (Mar 5, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. Hi team, Thanks for the great project, I've been using `better-auth` and I love it. I am now using `better-auth` with `react-router@v7` and my use case is to verify the request if it is authenticated or not on the server side. Is there any API that I can just pass in the `request` object and `better-auth` will tell if it is valid or not? **NOTE**: If `better-auth` doesn't have that before, I'm willing to contribute to it as I think this is a totally valid use case. Thank you ### Describe the solution you'd like Something like this: ```tsx const loader = async ({ request } : Route.LoaderArgs) => { const isAuthenticated = auth.verify(request) ... } ``` ### Describe alternatives you've considered No ### Additional context _No response_
Author
Owner

@krishna-santosh commented on GitHub (Mar 5, 2025):

Give this a try.

const session = await auth.api.getSession({
    headers: request.headers
});

if there's no session, it'll return null

@krishna-santosh commented on GitHub (Mar 5, 2025): Give this a try. ```ts const session = await auth.api.getSession({ headers: request.headers }); ``` if there's no session, it'll return null
Author
Owner

@thinh-trinh-duc commented on GitHub (Mar 5, 2025):

getSession({
headers: request.headers
});

Thanks @krishna-santosh!

@thinh-trinh-duc commented on GitHub (Mar 5, 2025): > getSession({ > headers: request.headers > }); Thanks @krishna-santosh!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#783