Can't accept invitation using acceptInvitation #1070

Closed
opened 2026-03-13 08:21:20 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @Marviuz on GitHub (Apr 17, 2025).

Originally assigned to: @ping-maxwell on GitHub.

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Follow steps from the docs to be able to create organization and send an invite link to an email.
  2. Click the link sent to the mail and trigger the acceptInvitation either front-end or back-end

Current vs. Expected behavior

Current

When using authClient, you'll get a response with
{ data: null, error: { status: 0, statusText: '' } }

When using auth

 ⨯ [Error [APIError]: ] {
  status: 'UNAUTHORIZED',
  body: undefined,
  headers: {},
  statusCode: 401,
  digest: '5381'
}

Expected

Able to accept user successfully and change invitation status from pending to the default accepted status (I currently don't know what it is)

What version of Better Auth are you using?

1.2.7

Provide environment information

- Windows 11
- Firefox (Zen browser)

Which area(s) are affected? (Select all that apply)

Backend, Client

Auth config (if applicable)

export const auth = betterAuth({
  plugins: [
    organization({
      ac,
      roles,
      cancelPendingInvitationsOnReInvite: true,
      creatorRole: 'admin',
      sendInvitationEmail: async (data) => {
        const res = await sendReactEmail({
          receivers: [{ email: data.email }],
          subject: `Invited by ${data.inviter.user.name}`,
          content: (
            <InviteLinkEmailTemplate
              expiration={data.invitation.expiresAt}
              inviteId={data.id}
              inviter={data.inviter.user.name}
              spaceName={data.organization.name}
            />
          ),
        });

        console.log('RESPONSE', res);
      },
    }),
  ],
  database: drizzleAdapter(db, {
    provider: 'pg',
  }),
  baseURL: BASE_URL,
  socialProviders: {
    google: {
      clientId: env.GOOGLE_CLIENT_ID,
      clientSecret: env.GOOGLE_CLIENT_SECRET,
    },
  },
});

Additional context

I followed the docs with some changes. Since the error is UNAUTHORIZED, the cause might be my custom roles.
This is my permissions.ts

import { createAccessControl } from 'better-auth/plugins/access';
import {
  defaultStatements,
  adminAc,
  memberAc
} from 'better-auth/plugins/organization/access';

export const ac = createAccessControl(defaultStatements);

export const admin = ac.newRole(adminAc.statements);
export const operator = ac.newRole(memberAc.statements);
export const participant = ac.newRole(memberAc.statements);

export const roles = { admin, operator, participant };

export type Role = keyof typeof roles;
Originally created by @Marviuz on GitHub (Apr 17, 2025). Originally assigned to: @ping-maxwell on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Follow steps from the [docs](https://www.better-auth.com/docs/plugins/organization#send-invitation) to be able to create organization and send an invite link to an email. 2. Click the link sent to the mail and trigger the `acceptInvitation` either front-end or back-end ### Current vs. Expected behavior ### Current When using `authClient`, you'll get a response with `{ data: null, error: { status: 0, statusText: '' } }` When using `auth` ``` ⨯ [Error [APIError]: ] { status: 'UNAUTHORIZED', body: undefined, headers: {}, statusCode: 401, digest: '5381' } ``` ### Expected Able to accept user successfully and change invitation status from pending to the default accepted status (I currently don't know what it is) ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash - Windows 11 - Firefox (Zen browser) ``` ### Which area(s) are affected? (Select all that apply) Backend, Client ### Auth config (if applicable) ```typescript export const auth = betterAuth({ plugins: [ organization({ ac, roles, cancelPendingInvitationsOnReInvite: true, creatorRole: 'admin', sendInvitationEmail: async (data) => { const res = await sendReactEmail({ receivers: [{ email: data.email }], subject: `Invited by ${data.inviter.user.name}`, content: ( <InviteLinkEmailTemplate expiration={data.invitation.expiresAt} inviteId={data.id} inviter={data.inviter.user.name} spaceName={data.organization.name} /> ), }); console.log('RESPONSE', res); }, }), ], database: drizzleAdapter(db, { provider: 'pg', }), baseURL: BASE_URL, socialProviders: { google: { clientId: env.GOOGLE_CLIENT_ID, clientSecret: env.GOOGLE_CLIENT_SECRET, }, }, }); ``` ### Additional context I followed the docs with some changes. Since the error is UNAUTHORIZED, the cause might be my custom roles. This is my `permissions.ts` ``` import { createAccessControl } from 'better-auth/plugins/access'; import { defaultStatements, adminAc, memberAc } from 'better-auth/plugins/organization/access'; export const ac = createAccessControl(defaultStatements); export const admin = ac.newRole(adminAc.statements); export const operator = ac.newRole(memberAc.statements); export const participant = ac.newRole(memberAc.statements); export const roles = { admin, operator, participant }; export type Role = keyof typeof roles; ```
GiteaMirror added the bug label 2026-03-13 08:21:20 -05:00
Author
Owner

@ping-maxwell commented on GitHub (Apr 20, 2025):

Can you show me your code which is calling the acceptInvitation function?

@ping-maxwell commented on GitHub (Apr 20, 2025): Can you show me your code which is calling the acceptInvitation function?
Author
Owner

@Bekacru commented on GitHub (Apr 22, 2025):

for auth.api make sure to provide headers and the client error is telling, you haven't configured your baseURL properly

@Bekacru commented on GitHub (Apr 22, 2025): for auth.api make sure to provide `headers` and the client error is telling, you haven't configured your `baseURL` properly
Author
Owner

@Marviuz commented on GitHub (May 2, 2025):

headers is exactly what I'm missing thank you.
Apologies for the delayed response. I was in a business trip.

@Marviuz commented on GitHub (May 2, 2025): `headers` is exactly what I'm missing thank you. Apologies for the delayed response. I was in a business trip.
Author
Owner

@agusgarcia3007 commented on GitHub (Jun 1, 2025):

Hey! Same issue here. what headers should i have to provide if I'm using better-auth from react vite? I want to do

authClient.organization.getInvitation(
      {
        query: { id: token },
      })

But I get 401 as user is not authenticated

@agusgarcia3007 commented on GitHub (Jun 1, 2025): Hey! Same issue here. what `headers` should i have to provide if I'm using better-auth from react vite? I want to do ``` authClient.organization.getInvitation( { query: { id: token }, }) ``` But I get 401 as user is not authenticated
Author
Owner

@adarshaacharya commented on GitHub (Jun 5, 2025):

Getting same error,
even passing headers shows unauthenticated..
Is there anyway for users to accept request without being signing in ?

Error processing invitation: [Error [APIError]: Not authenticated] {
  status: 'UNAUTHORIZED',
  body: [Object],
  headers: {},
  statusCode: 401
}
@adarshaacharya commented on GitHub (Jun 5, 2025): Getting same error, even passing headers shows unauthenticated.. Is there anyway for users to accept request without being signing in ? ``` Error processing invitation: [Error [APIError]: Not authenticated] { status: 'UNAUTHORIZED', body: [Object], headers: {}, statusCode: 401 } ```
Author
Owner

@ping-maxwell commented on GitHub (Jun 11, 2025):

Is there anyway for users to accept request without being signing in ?

Not at this time

@ping-maxwell commented on GitHub (Jun 11, 2025): > Is there anyway for users to accept request without being signing in ? Not at this time
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1070