Validate Client Side Auth On Server
Hi. I have a basic SPA written in react with a client side auth object to handle auth. Now i have other routes on my backend (with the auth server) which are used for different parts of the web app. How can i make sure the user is authenticated on the server for these routes. any help would be appreciated, and sorry if there is a straight forward answer but i couldn't find it. (Unless thats what a server useSession is for??)
Here is the code for the routes in question and the auth logic (taken from the get started docs)
Routes:
Auth Logic:
Solution:Jump to solution
you can use the export
auth
to make calls like auth.api.getSession
that you will pass the original request headers to, here is a NextJS but it will work on any framework (you will need to handle the headers differently based on your framework)
```ts
import { auth } from '@/lib/auth';
import { headers } from 'next/headers';...3 Replies
Solution
you can use the export
auth
to make calls like auth.api.getSession
that you will pass the original request headers to, here is a NextJS but it will work on any framework (you will need to handle the headers differently based on your framework)
see more documentation here
API | Better Auth
Better Auth API.
Thanks wasn’t sure just wanted to confirm. Appreciate it