How to Host better-auth on another Server
Hey,
How can I host a my App (domain.com) separate from a better-auth Server on e.g. (auth.domain.com)? And Optionally how can I use that better auth Server for Allowing sign-in to multiple different Apps (for example like Google, Google Docs, YouTube)
Thank you all
11 Replies
Hello @SiliconValley77
You can follow one of the Backend (e.g Hono) examples on your server and then only configure the client in your frontend.
It is not possible for you to make other websites use your authentication server/provider without their cooperation. If you on the other hand want to allow users to sign into your application using services like Google, you can do that using OAuth (e.g Google OAuth). Based on the credentials requested by your OAuth application, you can gain access do different Google services like Google Docs or YouTube on behalf of the user that consented.
First of all Thanks for your answer.
I meant the google stuff as an example and asked if I could build a similar system for my Projects. I have a few Sites and want them all to use the same AuthServer so when I log into one, I get logged into every other of my sites as well.
Sorry, I just read my question again and noticed it was not written very clearly
You can use one auth server for every one of your sites, which lets you share accounts on diffrent websites but sessions are stored per site.
In Technicality, there is probably some way or form for you get your desired behavior but I don't recommend it.
Oh okay. But can I call auth.api server side on my Next site and have it communicating with the auth server? Cause I couldn’t find anything in the docs
It doesn't matter where you call the api server (frontend or backend) as long as the taret server is your auth server.
I believe you specify the auth server in the client using the
baseUrl
Yes I read that but that does work with the server site of my App? I thought that was only client site
I just took a second peek at the docs and I have to excuse myself. I interpreted something wrong.
Yes you can apparently only use the client in client components as you were already aware of. Sorry for the confusion.
The Better Auth documentation does not explicitly state how to do backend calls to a sperate server. I suggest you try to create a new
betterAuth
object in your next app and specify the remote serverI also just stumbled upon this, which you might be interested in: https://better-auth.vercel.app/docs/concepts/cookies#cross-subdomain-cookies
Cookies | Better Auth
Learn how cookies are used in Better Auth.
No worries, I appreciate you taking the time to help! Thanks for checking the docs—I’ll take a look at the link you sent later. From what you mentioned, it sounds like a solid workaround I could use. Really appreciate the effort!
So are you planning on going client > server > auth server > server > client?
I'm sort of doing something similar but going directly from client to auth server.
One thing I thought about was just having all the server auth stuff with next / sveltekit in my case - then just specifying database url as my auth server. This should be possible as I'm using postgres but I am also running it behind a cloudflare tunnel which complicates things so I have not tried.
Off the top of my head a quick solution would be to use one database that i shared across your projects that stores your authentication data (in this case the better-auth generated schema) and have all of your backends for the different projects you have connect to this database for authentication.