Better-Auth With Different backend (FastAPI)

Hey guys, i want to use NextJS for its SSR but not use any of its server features. I will have a FastAPI backend as the main server. Can I use better-auth in this case? Im still unclear on where the server/client boundary is with all of this, new to nexjs in general. If I can use it this way, is it recommended? Thanks a lot!
20 Replies
The Untraceable
I don't think so because thats in python and better auth is in TS
maqi
maqiOP4w ago
Im not asking about using bgetter-auth in python, i know its a ts package. Im asking about having better-auth handle everything as it normally would, but for a way to validate auth it on the FastAPI backend, for example checking against the the session table, or something like that.
The Untraceable
Oh like checking auth thats handled by better auth in fast api?
maqi
maqiOP4w ago
ye ye That would be ideal
The Untraceable
Yea lemme just think for a sec
maqi
maqiOP4w ago
I like better-auth, i WANT to use it, i just need my api endpoints to be secured also
The Untraceable
Yea
maqi
maqiOP4w ago
Thanks man!
The Untraceable
Yea lemme js check and I'll get back to you Where are you gonna run better auth? On Nextjs?
maqi
maqiOP4w ago
Ye
The Untraceable
Yea aight one sec
The Untraceable
https://github.com/better-auth/better-auth/blob/canary/packages%2Fbetter-auth%2Fsrc%2Fapi%2Froutes%2Fsession.ts#L104-L111 I think this is the main auth check in better auth (could be wrong, kinda hard to see it all on mobile), you can make a function in python to verify the session cookie and that should work maybe
GitHub
better-auth/packages/better-auth/src/api/routes/session.ts at canar...
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
The Untraceable
Not sure how fastapi handles cookies (much prefer aiohttp.web) but you just need to verify the signature with the secret set in the app and make a db call with the decoded data
maqi
maqiOP4w ago
Amazing man, i will be looking into all of this, thank you so much
Arkuna
Arkuna4w ago
You probably want to use the JWT plugin, and use some kind of JWT auth library in python. I think better auth also got a jwks endpoint to get the public key in signing.
sebboer
sebboer4w ago
Maybe that helps you: I'm using better-auth as an authentication server and my .net backen is behind a reverse proxy (traefik). The reverse proxy uses a ForwardAuth to check the session. That check is just a custom endpoint in the better-auth server checking the session and responds with 200 or 401 to the reverse proxy.
The Untraceable
Yeah that works too, good idea. Much more simple And probably more performant because no DB calls
Timur
Timur4w ago
I am using Better-Auth with the JWT plugin to validate authentication and authorization within a .NET Web API Backend

Did you find this page helpful?