Better Auth

BA

Better Auth

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

Join

bug-reports

help

Stripe creating subscription: Unauthorized 401

Hello, I'm running into an issue with Stripe Subscriptions whereas I cannot generate a new subscription for an user. I'm using @better-auth/strip and receive a 401 Unauthorized when I'm creating a subscription on the client. The only weird thing AFAIK is that I use the convexAdapter, so I cannot run migrations for stripe myself (but this seems unrelated to the unauthorized). I've checked the .env.local keys a dozen times. I'm running a Tanstack Start app. auth.tsx...
Solution:
```typescript subscription: defineTable({ id: v.string(), // <-- this was the problem plan: v.string(), referenceId: v.id("user"),...

useSession returns null

Anyone knows why useSession returns null? 1) no cookie prefix or any cookie name customization 2) i can see cookie logs(in middleware) ```...
Solution:
thanks! i realize that when you render something depdening on the session data and it's null at the begining, it just stops there and wont continue
No description

Cloudflare Worker exceeded CPU time limit, while sign-up/email.

I was trying better-auth on cloudfare with hono and D1, and while I have a free plan on cloudflare that allows for up to 10ms CPU time per request, I am receiving Worker exceeded CPU time limit errors. This occurs during sign-up/email; I understand that sign-up involves multiple processes, including hono zod validation, among others. My concern is, do I need to upgrade my plan to get this working, or can I optimize my code or something? For example, I was thinking about using drizzleAdapter for authentication; could I change it to Kysely, or idk....
Solution:
It's very likely you will need to upgrade. In certain endpoints, such as sign up email, we will do multiple adapter calls to your DB, 1 call alone is most likely more than 10 ms alone, let alone maybe 3 calls....
No description

Getting FORBIDDEN (403) error when using authClient.admin

Auth config: ```export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "mysql" }), user: {...
No description

Organization: Determine what team a user belongs to

As the title implies, are members supposed to have a teamId associated to the model so that we can determine what team they belong to within an organization? I don't believe I see a teamId associated to the model. This is important as, when I fetch the full organization information, I want to be able to create team buckets but I cannot do that if I do not know what teamId values a member is associated with....
Solution:
The teamId conditional inference is inverted so it was being inferred when teams were not enabled and not inferred when teams were enabled I have made a pr to fix this https://github.com/better-auth/better-auth/pull/2133...
No description

How to revoke all sessions after reset password from forget password link?

I don't find any way to revoke all logged in sessions of a user after reset password from forget password link. I see there is an method authClient.revokeSessions() to do this but it need an loggedin sesison. Also i think about one other solution to login user using the email and newPassword but there is no way to get the email from the forget password link. Althought i can add an extra searchparam in the link before sending but i am looking for better way....
Solution:
this approach worked for me, i manually fetch the user email using the userId, then first login the user and then revokeOtherSesion using authClient. Thanks...

session schema is missing

i have added the secondaryStorage in auth config, but also enabled this session check to store the session in database but still the better-auth cli is not generating the session schema,
No description

Slow API response time until sign out & sign in again

I'm having a weird issue with slow response times. I'm integrating Better-Auth with my Next.js app and I noticed that if I'm signed in to my dashboard and then stop and start the Next.js dev environment again using pnpm run dev my api calls are super slow - around 1 ~ 3 seconds. This happens only when I stop the server (without signing out) and start it back again. The app seems to be super slow. What seems to fix it is to sign out and sign in again, then it gets back to nromal and the GET calls drop do 20 - 250ms. Until of course the next time when I stop the dev server and start it back again. Did anyone else noticed such issue?...
Solution:
I don't think there is much else you can do. Alternatively you can setup secondary storage and use something like Redis from upstash if you want sessions to get to your client faster....
No description

Error on Signup with OTP

//auth.ts ```ts import { betterAuth, BetterAuthOptions } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { admin, emailOTP, openAPI, twoFactor } from "better-auth/plugins";...

Issue with Session Revocation and Middleware in Next.js

I’m trying to revoke a user session in a Next.js app using middleware, but I’m running into a problem. After calling revoke-sessions and redirecting the user (which triggers the middleware again), get-session still returns a session—even though it’s supposed to be revoked. This causes an infinite redirect loop (too many redirects). Is it possible to properly delete or invalidate sessions from within the middleware? Or is there a better way to handle session revocation and redirection flow?...
No description

Nextjs middleware not working

In this i am using the getSessionCookies() but the sessionCookie returns as null i am using the cookiePrefix also ...
No description

First Sign In

What's the best hook to catch first sign in of a user ?

Expo Google login

I'm using better-auth with Expo for Google social login. In the socialProviders config, I see that Google requires both clientId and clientSecret, but as far as I understand, when using the Android client (for mobile), Google doesn't provide a clientSecret. If I try to leave it as undefined, I get a type error. ...
Solution:
Oh wait, if you’re authenticating in the expo app, you should get idToken and you should use id token auth instead.

Authentication with Express backend + Next.js frontend

This is my first time using Better Auth, so forgive me if this is obvious 😅 I have an existing backend built with Express, which has Better Auth setup and running. We are creating a new Next.js frontend, on a separate server, which needs to authenticate against this backend. ...
Solution:

Plugin to extend organization plugin

Hello everyone 👋 I'm building a plugin to extend organizations by making sub-organizations. The idea is that I want to extend organization schema to include sub-organizations to make it one-to-many (one organization to many sub-organization) how can I achieve such thing? I'm thinking of auth.ts:...

annotations forbetterAuth and createAuthClient

It was clean but I think I fuc-ked up typescript by updating it? both betterAuth and createAuthClient throw annotations errors. The inferred type of 'betterAuthClient' cannot be named without a reference to '@/node_modules/better-auth/dist/shared/better-auth.BAfIsot3'. This is likely not portable. A type annotation is necessary.ts(2742)...
Solution:
Just dealing with this myself, you can: 1. if your project is not a library, set declaration and declarationMap to false in your tsconfig 2. if it is a library, you're probably exporting a function that needs it's return type annotated. Good thread with some solutions here: https://github.com/pnpm/pnpm/issues/6089...
No description

Rate Limit doesn't work for /send-verification-email

I am trying to configure rate limiting in Next.js, but for some reason it doesn't work. I tried in both dev and prod environments, with the following config: `rateLimit: { enabled: true, customRules: {...
Solution:
Okay apparently calling auth.api.sendVerificationEmail from server bypasses the rate limitations, it only works when request is coming from the client using authClient.sendVerificationEmail

Cannot set additionalField when calling signUp.email

I'm trying to pass a referredBy field during user registration using signUp.email, but TypeScript is rejecting it with the following error: "Object literal may only specify known properties, and 'referredBy' does not exist in type " 1. I've properly configured referredBy in my auth.ts configuration:...
Solution:
I had plugins indented

Importing TWO_FACTOR_ERROR_CODES – Is It Possible?

Hello, is it possible to import TWO_FACTOR_ERROR_CODES from the file plugins/two-factor/error-code.ts? Github link...
Solution:
Not right now, but I just made a PR to fix this. https://github.com/better-auth/better-auth/pull/2102...

Discord auth scopes

Hii can i configure the scopes i want while authorizing the user using better auth?
Solution:
To give you an example: