Authentication with Express backend + Next.js frontend
This is my first time using Better Auth, so forgive me if this is obvious 😅
I have an existing backend built with Express, which has Better Auth setup and running.
We are creating a new Next.js frontend, on a separate server, which needs to authenticate against this backend.
After reading the docs, seems like authenticating on the Next.js client side is fairly simple using the
better-auth/react
package, but trying to get the session on the Next.js server side requires importing the auth
instance, which I cannot access since its on our Express backend.
What is the solution if I need to make a server side fetch to our Express backend?Solution:Jump to solution
Well, I followed this for server-side session:
https://www.better-auth.com/docs/integrations/next#for-nextjs-release-1517-and-below...
https://www.better-auth.com/docs/integrations/next#for-nextjs-release-1517-and-below...
Next.js integration | Better Auth
Integrate Better Auth with Next.js.
5 Replies
well for server side sessions on nextjs. you can make an api request to the endpoint on express backend like this. BTW i have created a custom hook which gives the session both on client/server side basically checking if window global is defined or not

I tried this as a quick test, and it seems to work. Wondering if this is a valid approach?
Solution
Well, I followed this for server-side session:
https://www.better-auth.com/docs/integrations/next#for-nextjs-release-1517-and-below
https://www.better-auth.com/docs/integrations/next#for-nextjs-release-1517-and-below
Next.js integration | Better Auth
Integrate Better Auth with Next.js.
You can make a fetch request to your auth server endpoint with the needed headers/body/etc.
I recommend using the OpenAPI plugin to see all endpoints as well as the required headers/body to do the calls.
Thank you @Ali Nasir @Ping