Better Auth

BA

Better Auth

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

Join

bug-reports

help

better-auth Session Not Recognized After Manual Creation in Telegram Callback

Telegram successfully authenticates, and my callback verifies the data. However, I'm manually creating the session record in the DB and setting the session cookie afterwards. Below how I'm doing it in my /api/auth/callback/telegram.ts. After this manual setup and redirecting to a protected route, my middleware check await auth.api.getSession(...) fails to recognize the session, causing a redirect back to /signin. My Question: Is manually inserting into the session table and setting the better-auth.session_token cookie like this the correct approach for integrating a custom callback (like Telegram's data-auth-url) with better-auth?...

how to customize/translate BASE_ERROR_CODES

and how to replace or customize the error page of better auth?
Solution:
and how to replace or customize the error page of better auth?
You can pass this to your better-auth config to change the error page: ```ts onAPIError: { errorURL: "/my-err-url"...

Stripe plugin status properties full list?

Is there a comprehensive list of the status schema properties in the subscription table? It looks like better-auth manages this column so I can't customize them. I see (active, canceled, etc.) in the docs, and 'trialing' comes up in my db. Are there any others? 'inactive'? 'reset'?
Solution:
@Ham

How to fix Stripe error

[Better Auth]: Error fetching subscription from Stripe RangeError: Invalid time value at Date1.toISOString (<anonymous>) at Array.map (<anonymous>) at Array.map (<anonymous>) at Array.map (<anonymous>)...
Solution:
I downgraded Stripe to version 17.7.0, and it works!

How can I get a user from an api key ?

Hey all, I recently stumbled onto better-auth and really like it, especially once I saw it had a plugin for api keys 😍 I managed to get it set up and can create api keys from nuxt (on client side), but I cannot seem to get a user out of them ? I can see the userId is set on the key (since it's created from an authenticated user), but if I try to get a user out of the session by doing this:...
Solution:
Ok, it just seems like I cannot copy/paste correctly into a curl command. Nothing to see here xD

How to access user email or userId in social login hook (Google/Microsoft)?

Hi everyone! 👋 I’m trying to handle user logins using Google and Microsoft in BetterAuth. In the after hook of createAuthMiddleware, I want to check some conditions based on the user’s email or userId, like I already do for regular email sign-in (see screenshot). ...
Solution:
All good, I resolved my issue by using ctx.context.newSession and ctx.redirect 👍
No description

Can you change callbackURL with requireEmailVerification default option?

Hey! Im not calling sendVerificationEmail directly, is there anywhere where I can configure what the default requireEmailVerification option is doing to change the callbackURL ?

How to run the client on a server route as admin

Hi, I am using Nuxt and have the admin plugin installed. I want to List all users, which is possible If the user doing the query is of role "admin". But if not, the query fails, which is correct. But, how would I do this in a server environment? I need to check in a server route if a user exists, how do i do this? If I dont pass headers, I get 401, If I do, I get 403....

Even being owner, getting YOU_ARE_NOT_ALLOWED_TO_INVITE_USERS_TO_THIS_ORGANIZATION

I defined owner permission set as ``` export const ownerRole = accessControl.newRole({ customer: ["create", "read", "update", "delete"], product: ["create", "read", "update", "delete"],...
Solution:
got it sorted Ping, I guess, from now onwards, we have to explicitly specify invitation: ["create"] in permissions set, and it is not part of defaultStatements , I am happy to add this explicitly, I am good now.

Team metadata

Hello! I am using the metadata field in Organizations to organize them using ids, since the app is intenationalized. The team schema, however, is missing a metadata field. How can I add metadata to it? Any help would be appreciated!...

Express returns 404 on /then, but responds to /ok

Hi, i can't seem to figure out how to properly set up better auth with express. After following the guide i seem to get an {"ok":true} on the /ok endpoint. When i tried using the react client it called the /then endpoint, which returned a 404. Same thing happens when sending a basic get request. I'd appreciate some help figuring out what could be wrong...

Issues with Skeleton Loading Not Displaying for Session-Based Components

I'm encountering an issue with rendering a skeleton loading state in my React components that depend on the user's session. I've created custom SignedIn and SignedOut components to conditionally display content based on whether the user is authenticated. The problem arises when I refresh the website—the skeleton loader for the UserAvatar component doesn't show as expected during the pending state; instead, the component seems to render as null and immediately displays the UserMenu. Here's a brief overview of my implementation: - SignedIn Component: Displays its children only when there is an active session. - SignedOut Component: Renders its children only when there's no active session and the session is not pending....
No description

Sign out after revoking current session

After revoking the current session, getSessionCookie in the middleware still return a value so it's act like the user is signed in. How can I prevent this ?

Ratelimit with Email Verification

Hi, everyone! Can someone tell me why I'm not able to set rate limit to sendVerificationEmail separatley? This rules are applied in such way that if i have different rate limit for sign-in with email and requireEmailVerification: true, emails will be sent as many times as sign-in rate limit allows to call it...

Clarification on Email OTP Auth Flows

I am not clear on the flows of the Email OTP auth flows. 1. SignIn with OTP - I have regular credentials setup. Seems like it replaces the password in credentials? And I would have only an email field with a button Sign In with Passcode? Or does this happen in addition to password kinda like two-factor auth without the twoFactor plugin. 2. Verify Email - self explanatory. Sent on Sign Up, user auto Signed In. Authenticated Form to input OTP and resend verification email button. 3. Reset Password - Is this for currently logged out user it seems? Reset password form with just email field. This email gets sent with the OTP and a link to your public password change password form: otp, email, new password, confirm new password? I'm generally familiar with reset links. But I think somewhere in OWASP, OTP are recommended....

Redirect for first time user with social sign in

How would I change the redirect for a first time user signing in with a social provider, since they are all sign ins?
Solution:
const data = await authClient.signIn.social({
provider: "google", // or any other provider
callbackURL: "/dashboard", // URL for existing users
newUserCallbackURL: "/onboarding", // URL for first-time users
});
const data = await authClient.signIn.social({
provider: "google", // or any other provider
callbackURL: "/dashboard", // URL for existing users
newUserCallbackURL: "/onboarding", // URL for first-time users
});
...

Drizzle and better auth problem

Hello, any one knows the reason why im viewing this error? my schema is this
No description

How can I complete some actions on OAuth authorization before signing the user in?

I'm developing a web application where I need to create certain resources and store them in my database as part of the signup process. If this resource creation fails, I want to return an error and prevent the signup from completing. How can I achieve this when registering via. OAuth?

What is the best approach when handling the protected and unprotected routes

I designed a layout for the sign in and sign up pages with the goal of redirecting any authenticated user straight to the home page (/). However, when I navigate to either the sign in or sign up page while already having an active session, the page content briefly renders before the redirect occurs. Additionally, I encounter the following error:
Cannot update a component (`Router`) while rendering a different component (`AuthLayout`). To locate the bad setState() call inside `AuthLayout`, follow the stack trace as described in https://react.dev/link/setstate-in-render
Cannot update a component (`Router`) while rendering a different component (`AuthLayout`). To locate the bad setState() call inside `AuthLayout`, follow the stack trace as described in https://react.dev/link/setstate-in-render
...
Solution:
``` import { headers } from 'next/headers'; import { redirect } from 'next/navigation'; import { auth } from '@/lib/auth';...