Better Auth

BA

Better Auth

Join the community to ask questions about Better Auth and get answers from other members.

Join

bug-reports

help

How do you let the user choose which Google account to use when using social sign-in?

Currently, when I click "Sign in with Google" it just logs in to the last account I used. Is there a way to have me choose which account to use via a pop-up window?

How do I implement authentication for tenant's clients with better-auth ?

I have an b2b2c app based on vercel's platform starter with an basic auth implementation for tenants..however I am not sure how should I implement auth for tenants clients...any guide or resources would be reaally helpful .. I am sharing the example repo below ... https://github.com/ra-kesh/stayin-platform...

Oauth in GoogleAddon

We are using better auth for a web app and a google addon so in web app we can use cookie based oauth login but in addon we can't, so we were trying to use bearer plugin but we are not able to get the 'set-auth-token' on onSuccess handler, i have tried onResponse handler also. Also i don't know if bearer plugin is good fit for the google addon because it also sets the cookie. What can we do in this case?...
Solution:
so when you use social logins we can't set cookies cause it returns back to your back with redirect headers. What you should do is issue send the token as a part of redirect url ```ts export const auth = betterAuth({ hooks: { after: createAuthMiddleware(async(ctx)=>{...

Update email of existing user entry that was created initially by verifyPhoneNumber API

I have an existing session already and I want that after I verify the email via magicLinkVerify API, the email of that user is updated rather than a new user being created altogether. Is that possible? I was only able to "merge" users when I first created a user with magicLinkVerify API AND THEN verifyPhoneNumber API to which the same existing user is updated (see second screenshot)...
No description

App infinitely reloading and not sure why.

In my Astro app i am using a simple middleware following the example in the docs and for some reason when i am on my root page it infinitely reloads the page, the issue seems to be happening when calling:
const isAuthed = await auth.api.getSession({
headers: context.request.headers,
});
const isAuthed = await auth.api.getSession({
headers: context.request.headers,
});
...
Solution:
I am was able to figure this out, when we try and auth a request we send it to /api/auth/* to get the session however the middleware will run on this meaning we will end up in an infinite loop due to it calling itself over and over again, not a problem on certain pages but possibly on others. my reworked middleware.ts ```ts import { auth } from "@/lib/server/auth"; import { defineMiddleware } from "astro:middleware"; ...

Client-side user impersonation in Better-Auth with OpenAPI-TS: Why does it fail?

Hi everyone, I need a hand implementing a method for updating a user's data from another user with elevated permissions. I could get my hands a little dirty creating my own endpoint or service to do this, but I think the tool will have a better way to do it. I'm currently creating a basic dashboard to get an idea of ​​how to use the tool built with React Router 7 (REMIX.js)....

Validate invalid token on reset password

If i sendResetPassword url and click it - token is validating and if is valid redirect to provided url if not returning query params with error. It's ok. But what if i enter to to reset password page and I provide bad token manualy? I don't know if my token is valid or not? How can i check it? I think checking in client.resetPassword({newPassword, token}) is too late....

Better auth does not work on NextJS

Hi guys, im trying to add Better Auth to my empty nextjs project but it does not work. I copied the demo project and it does work there, i copied every auth related file but i still cant get it to work in my project. im trying to check the session on a server component like this but the session is always null. I have not added a database as i dont need one, im logging in using my google account. I keep getting redirected to /sign-in and the console shows this error: ```bash 2025-04-20T19:46:49.619Z WARN [Better Auth]: No database configuration provided. Using memory adapter in development [Error [APIError]: ] {...

Stripe plugin triggers multiple create customer events in Stripe

I have 2 different buttons to subscription.upgrade in my app. After user is registered customer is created in stripe according to createCustomerOnSignUp: true, in plugin config. But when I navigate to one of my 'Subscribe' buttons and click on it -> subscription.upgrade just creates new user in stripe instead of triggering checkout session: ```sh 2025-04-20 14:38:59 --> customer.created [evt_1RG3hf4FS9Rcg327BHPwlIez] 2025-04-20 14:38:59 <-- [200] POST http://localhost:3000/api/auth/stripe/webhook [evt_1RG3hf4FS9Rcg327BHPwlIez]...

Issue with Better-Auth Email Verification

Hey everyone, I'm having an issue with email verification in better-auth. When a user changes their email: The email address gets updated correctly in the database BUT the verification email is not being sent...

Organizations: remove activeOrganization possible?

Hey there, is there a way to remove the activeOrganization so users could decide to be in a active Organization? For my example i'm using the Organization Plugin to have personal and organization workspaces. But my problem is it sets a activeOrgId when I'm joined into a minimum of one organization so users aren't able to "escape" the organization workspace....
Solution:
you can set it null

Cookies not saved in production

I have a node/express backend and a vite react frontend. They are on different urls: Backend: http://my-backend.vercel.app/ auth.ts looks like this:...

clearing 2FA cookie on logout

Hi guys, I’m using Better-Auth with TOTP-based 2FA and had a question about the better-auth.two_factor cookie behavior. I’ve noticed that after a user successfully verifies their TOTP code, the better-auth.two_factor cookie remains present in the browser — even after a full session is created. Similarly, it doesn’t appear to be cleared when the user logs out. This leads to a UX issue where, after logout, my middleware sees the cookie still present (but no session), and incorrectly redirects users back to /verify-2fa instead of letting them hit /....

See what provider made the session

Is there a way to see what provider triggered the session creation? I need to restrict some stuff based on which provider you used to log on due to business restrictions. Or do you have any tips for a workaround....

Setting an `apiKey` additional field on users

I'm trying to add an apiKey field to my users, like so: ```ts import { betterAuth } from "better-auth"; export const auth = betterAuth({...
Solution:
I'm going assume you're using the apiKey plugin.
But I'm not sure how to generate an unique key for each user on signup. How can I do this?
Use database hooks: https://www.better-auth.com/docs/concepts/database#2-after-hook...

custom type in additionalFields

is it possible to use custom type in additionalFields?
Solution:
We provide set types and that's all the supported for our adapters.
No description

Centralized auth

Is there any way to have multiple (NextJS + Hono) apps, and a centralized auth one? So every app redirects to auth.example.com and gets the auth form from there and authenticates in there? Thanks in advance
Solution:
so you will have one instance on server either of the server framework and client frameowrk to create a client instance separetly so that they can make a request to server with through better auth client abstraction

useSession weird behaviour

Hi, I have some problems with useSession, it keeps giving me date null even though I am logged in. I'm testing it with dev and turbopack. This happens when I do a page refresh, so if I logout and log in the hook works, but if I do a page refresh it no longer works returning null. auth-client.ts...
No description

List of endpoints?

Can’t seem to find it, but just looking for a list of endpoints that better with exposes by default?

Can I use better auth as authentication layer between hono backed and next js frontend?

I'm building platform that has hono js as backend, next js for web app, expo for mobile, prisma for db within turborepo. I'm using nextjs server side data fetching so I have to check authorisation on server side of next js. I know this case is too much complicated. Please if you have any near examples or suggestions, It would help a lot....