Adding Custom App Name, throws UNAUTHORIZED Api Error #188

Closed
opened 2026-03-13 07:36:54 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @sohamnandi77 on GitHub (Nov 6, 2024).

Describe the bug
When attempting to set a custom appName in the betterAuth configuration, the API returns an UNAUTHORIZED error. The setup should allow for the customization of appName without impacting the authorization flow.

To Reproduce
Steps to reproduce the behavior:

  1. Configure betterAuth with a custom appName (e.g., Next Auth).
  2. Attempt to use the signUp or signIn methods.

Expected behavior
Users should be able to sign up or sign in without encountering authorization errors when a custom appName is provided.

terminal error

 ○ Compiling /dashboard ...
 ✓ Compiled /dashboard in 965ms
 ⨯ [Error [BetterCallAPIError]: API Error: UNAUTHORIZED ] {
  status: 'UNAUTHORIZED',
  headers: Headers { 'Content-Type': 'application/json' },
  body: {},
  digest: '3161027971'
}

auth.ts

export const auth = betterAuth({
  appName: "Next Auth",
  database: {
    dialect,
  },
  emailAndPassword: {
    enabled: true,
    autoSignIn: true,
    minPasswordLength: AUTH_CONFIG.MIN_PASSWORD_LENGTH,
    maxPasswordLength: AUTH_CONFIG.MAX_PASSWORD_LENGTH,
    resetPasswordTokenExpiresIn: AUTH_CONFIG.RESET_PASSWORD_TOKEN_EXPIRES_IN,
  },
  logger: {
    disabled: env.NODE_ENV !== "development",
    verboseLogging: env.NODE_ENV === "development",
  },
});

auth.client.ts

import { createAuthClient } from "better-auth/react";
import { toast } from "sonner";

export const client = createAuthClient({
  fetchOptions: {
    onError(e) {
      if (e.error.status === 429) {
        toast.error("Too many requests. Please try again later.");
      }
    },
  },
});

export const { signUp, signIn, signOut, useSession } = client;

middleware.tsx

  const { data: session } = await betterFetch<Session>(
    "/api/auth/get-session",
    {
      baseURL: request.nextUrl.origin,
      headers: {
        //get the cookie from the request
        cookie: request.headers.get("cookie") || "",
      },
    }
  );
Originally created by @sohamnandi77 on GitHub (Nov 6, 2024). **Describe the bug** When attempting to set a custom appName in the betterAuth configuration, the API returns an UNAUTHORIZED error. The setup should allow for the customization of appName without impacting the authorization flow. **To Reproduce** Steps to reproduce the behavior: 1. Configure betterAuth with a custom `appName` (e.g., Next Auth). 2. Attempt to use the signUp or signIn methods. **Expected behavior** Users should be able to sign up or sign in without encountering authorization errors when a custom `appName` is provided. terminal error ``` ○ Compiling /dashboard ... ✓ Compiled /dashboard in 965ms ⨯ [Error [BetterCallAPIError]: API Error: UNAUTHORIZED ] { status: 'UNAUTHORIZED', headers: Headers { 'Content-Type': 'application/json' }, body: {}, digest: '3161027971' } ``` auth.ts ```tsx export const auth = betterAuth({ appName: "Next Auth", database: { dialect, }, emailAndPassword: { enabled: true, autoSignIn: true, minPasswordLength: AUTH_CONFIG.MIN_PASSWORD_LENGTH, maxPasswordLength: AUTH_CONFIG.MAX_PASSWORD_LENGTH, resetPasswordTokenExpiresIn: AUTH_CONFIG.RESET_PASSWORD_TOKEN_EXPIRES_IN, }, logger: { disabled: env.NODE_ENV !== "development", verboseLogging: env.NODE_ENV === "development", }, }); ``` auth.client.ts ```tsx import { createAuthClient } from "better-auth/react"; import { toast } from "sonner"; export const client = createAuthClient({ fetchOptions: { onError(e) { if (e.error.status === 429) { toast.error("Too many requests. Please try again later."); } }, }, }); export const { signUp, signIn, signOut, useSession } = client; ``` middleware.tsx ```tsx const { data: session } = await betterFetch<Session>( "/api/auth/get-session", { baseURL: request.nextUrl.origin, headers: { //get the cookie from the request cookie: request.headers.get("cookie") || "", }, } ); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#188