First Timer - Understanding Client vs Server
Hi guys! I just met Better-Auth after a friend recommended it to me and I'm really excited to try it out!
So far I tried using it with NextJS from a server only perspective (I'm also learning Nextjs), but I got a bit confused about the Client/Server boundaries.
In the Basic Usage section of the Documentation, it starts with:
"To sign up a user you need to call the client method signUp.email with the user's information."
I'm assuming thats only if you're working on client side, is that correct?
So all those callbacks the Docs talk about, like "onRequest", "onSuccess" and "onError" are only for client side, I assume.
But can I have these callbacks on the server side as well?
How should I use Better Auth on client vs server?
Also, this is not really tied to Better-Auth but doesn't hurt to ask: Is it possible to build a completely server side Next app? For example: Can I show logout button dynamically on navbar using only server code?
4 Replies
Everything in Better-Auth works around endpoints, and we've designed everything to be client-first, meaning it's expected that most if not all endpoints will be called from the client. The choice between server vs client does depend, if possible, use the client. But otherwise if you need to do stuff like validation on certain things, you can run that validation logic on the server followed by calling the server side methods. (Although I would personally recommend running validation logic through hooks )
Also, this is not really tied to Better-Auth but doesn't hurt to ask: Is it possible to build a completely server side Next app? For example: Can I show logout button dynamically on navbar using only server code?Yes it's possible, however managing things such as cookies will start to become a pain, or at least more painful than necessary. Also anything reactive, such as the need to have a
useSession
hook in your UI will no longer be possible if everything is server-side, and so you won't have any reactive auth in your UI making this almost impossible to work, unless your app is not very interactive and have just some static app like a blog.
Try not to be psyoped into thinking that you need to run as much as possible on the server, it's not a bad thing to run code on the client. Having more logic on the server is more compute work on your end and doesn't necessarily provide more performance all while leading to high compute billing if not thought through.Hooks | Better Auth
Better Auth Hooks let you customize BetterAuth's behavior
ahh i see! thank you very much for the answers
so if im not using client side, i dont need the [...all] api endpoints?
tbh, im starting to consider going back to react only (full client side), just cuz juggling this client/server thing is kinda of a pain yeah
Nope, if you're just using auth.api everything should work fine without it
i see i see, thank you very much! that definitely clear things up for me