Multiple apps sharing the same database using Better Auth

Hello! You guys are doing some awesome stuff with this framework. I am wondering if it is possible to have the following setup: - Postgres database - Nextjs app - Honojs api I want to share the same database between the nextjs app and honojs backend api using Better Auth. For example I would setup a Better Auth server on both nextjs app and honojs, configuring the same database. I would share the same Auth secret. Alternatively I would manually verify the session on the honojs backend api directly in the database rather than using Better Auth. Do you have any recommendations for solving this problem? I need to have a backend api in addition to a nextjs app as I will be managing large numbers of web socket connections. The websockets will be from expo of that makes any difference. Any thoughts are appreciated! Thanks!
12 Replies
Ping
Ping•this hour
Yes it's do-able. I assume from this you want to setup better-auth on both nextjs as well as hono though? You can always have the option to only setup your auth server on Hono, and just use the better-auth client on Nextjs, and then route the auth request to your hono api
Thorlon
ThorlonOP•22h ago
Gotcha - yeah it would be on both hono and nextjs. Client -> NextJS -> Postgres Mobile Client -> Hono -> Postgres. Postgres bring the same database instance. Your suggestion is: Client -> NextJS -> Hono -> database? Where i forward the requests through to Hono or potentially directly to Hono and have NextJS ssr lookup using the Better Auth client?
Ping
Ping•22h ago
Yeah, so essentially your main auth server is on Hono, and then both your nextjs app and your mobile app can "talk" to your Hono auth server. All you need to do on Nextjs is setup the createAuthClient and then set the baseURL to be your hono url
Thorlon
ThorlonOP•22h ago
Nice! I will give it a try! Thanks! That will work in nextjs server actions and server side rendering?
Ping
Ping•22h ago
SSR should work fine, but not certain on server actions. The issue with server actions is we can't forward cookies over without the nextCookies plugin, but I'm not sure how that might play out if your auth server is also in-use for mobile app as well... Not too sure.. you may need to test around and see 👀 In most cases, I see server actions pretty pointless for stuff like signin/signup
Thorlon
ThorlonOP•22h ago
Makes sense - thanks for your quick replies! I’ll let you know how I get on.
NeoPrint3D
NeoPrint3D•15h ago
if you need help i have a mono repo doing tbis except it is with astro js
NeoPrint3D
NeoPrint3D•15h ago
GitHub
GitHub - NeoPrint3D/neostack
Contribute to NeoPrint3D/neostack development by creating an account on GitHub.
Thorlon
ThorlonOP•13h ago
Amazing this is super helpful!
Dominik
Dominik•13h ago
Hey @Thorlon noticed you use the same stack as me did you also run into this issue #Expo plugin isn't a valid plugin?
NeoPrint3D
NeoPrint3D•13h ago
no problem
Thorlon
ThorlonOP•11h ago
Not having that issue. Was able to set it up and run better-auth on expo with no issues.

Did you find this page helpful?