Better Auth

BA

Better Auth

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

Join

bug-reports

help

2FA Totp

Hello, i wanna add 2fa TOTP to credentials login. How can i generate a QR code for user to scan ?
Solution:
That's out of Better-Auth's scope at what we provide. You should find a library that does this
No description

Get session data on first load (nextjs + elysia)

Is there a way to retrieve the session data on the first load using useSession? I already using middleware to validate the session. For my pages, I want to display the user data immediately without showing the loading.

Get session

Hello, im trying to redirect not authenticated user to login page. But it does not redirect me My code: ```tsx...
Solution:
you need to use authClient.useSession not getSession

Better Auth + MySQL - Failing Schema Generation

Hey guys, I have been trying to get better-auth up and running with my existing NextJS project. To do so I have been following the instructions in the better auth documentation: (https://www.better-auth.com/docs/adapters/mysql). Despite that I am unable to generate the respective schemas using the npx @better-auth/cli@latest generate command. In my project I use yarn as my package manager. For whatever reason trying to install the mysql2/promise package, as per documentation, already fails. ...
Solution:
Thank you for your help. I was able to fix the issue, which boils down to very basic mistake / misunderstanding. While I had tried to install the mysql2 package, I then also updated the import statement to "import {createPool } from "mysql2", which then lead to an error once again. It now works as expected.

Session cookie not refreshing

I'm using Next.js Pages Router. Login works fine. Session gets updated on server when it's supposed to, pushing back the expiresAt. But the response headers for all the later requests don't include the session cookie, so it expires and gets removed. I've tried calling authClient.getSession to try to manually refresh the session - returned 200 but didn't include the session cookie in the response headers either. I'm not using the customSession plugin....
Solution:
yeah it is fixed on 1.2.6

Steam Social Login - Open ID 2.0

Steam is the main auth provider for my app. I am exploring how to integrate it with Better Auth to make my auth more robust. Steam uses Open ID 2.0 which is older than OpenID Connect and the associated Generic Oauth Plugin . I have a direction that I think I can go with a custom plugin approach but I want to check in with the community to see if anyone has a solid existing plugin/strategy for integrating steam....

Better Auth Api Key Sessions

what is the correct way to use the api key plugin to create a session for the user? Basically we have a nextjs application and want to generate api keys which the user can access our application (dashboard) with. Once api key is provided in login we would somehow need to create a session.

Hono API on Lambda

Has anybody ever made a hono API connecting to better-auth deployed on AWS Lambda. I have tried following the documentation and am getting 404s on all routes associated with better-auth. I am at a loss here and would love to see a working example if anyone in the community is willing to share!🙏

Middleware - Better Auth

Hello everyone! I'm having an issue where my /dashboard route is not being protected even when the user isn't logged in. I'm following the documentation: https://www.better-auth.com/docs/integrations/next#middleware...
No description

Logging In as a different user

We run events in which there are a few hundred people that log in to our app at once in the same location. We've faced issues that some users login successfully with their credentials but they are logged in as someone completely different. We also don't have caching enabled. Do you guys know why this might be happening?

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