[GH-ISSUE #8932] auth.api.setActiveOrganization returns an error when called with a valid org id #11241

Open
opened 2026-04-13 07:35:12 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @chrishj59 on GitHub (Apr 3, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8932

Originally assigned to: @ping-maxwell on GitHub.

Is this suited for github?

  • Yes, this is suited for github

Reproduction

Configure auth as:
export const auth = betterAuth({
appName: 'Rationes-Charitatis',
database: zenstackAdapter(db, {
provider: 'postgresql',
}),
user: {
additionalFields: {
role: {
type: 'string',
required: false,
defaultValue: 'user',
input: false, // don't allow user to set role
},
displayName: {
type: 'string',
required: true,
defaultValue: ' ',
input: true,
},
firstName: {
type: 'string',
required: true,
defaultValue: ' ',
input: true,
},
familyName: {
type: 'string',
required: true,
defaultValue: ' ',
input: true,
},
},
},

emailVerification: {
// async sendVerificationEmail({ user, url }) {
// console.log(
// About to send aws verification email to ${user.email} at url ${url},
// );
// const resp = await awsVerificationEmail(user.name, user.email, url);
// console.log(AWS resp ${JSON.stringify(resp)});
// // const res = await resend.emails.send({
// // from,
// // to: to || user.email,
// // subject: 'Verify your email address',
// // html: <a href="${url}">Verify your email address</a>,
// // });
// // console.log(res, user.email);
// },
// sendOnSignUp: true,
async sendVerificationEmail({ user, url }) {
console.log('Sending verification email to', user.email);
const res = await resend.emails.send({
from,
to: to || user.email,
subject: 'Verify your email address',
html: <a href="${url}">Verify your email address</a>,
});
console.log(res, user.email);
},
sendOnSignUp: true,
},
emailAndPassword: {
enabled: true,
async sendResetPassword({ user, url }) {
await resend.emails.send({
from,
to: user.email,
subject: 'Reset your password',
react: reactResetPasswordEmail({
username: user.email,
resetLink: url,
}),
});
},
},

plugins: [
organization({
schema: {
organization: {
// name: 'Trading name',
customRoles: [
{ role: 'orgAdmin', label: 'Organisation Admin' },
{ role: 'fundAdmin', label: 'Fund Administrator' },
{ role: 'auditor', label: 'Auditor' },
{ role: 'salesSuper', label: 'Sales Supervisor' },
{ role: 'salesInvoice', label: 'Sales invoice' },
{ role: 'salesCash', label: 'Sales cash' },
],
additionalFields: {
tradingName: {
type: 'string',
input: true,
required: true,
},
legalForm: {
type: 'string',
input: true,
required: true,
},
legalName: {
type: 'string',
input: true,
required: true,
},
charityNumber: {
type: 'string',
input: true,
required: false,
},
taxRef: {
type: 'string',
input: true,
required: false,
},
companyNumber: {
type: 'string',
input: true,
required: false,
},
companyName: {
type: 'string',
input: true,
required: false,
},
idType: {
type: 'string',
input: true,
required: true,
},
identification: {
type: 'string',
input: true,
required: true,
},
trial: {
type: 'boolean',
input: true,
required: false,
},
trialStart: {
type: 'date',
input: false,
required: false,
},
accountType: {
type: 'string',
input: true,
required: false,
},
},
},
},
async sendInvitationEmail() {
// const res = await resend.emails.send({
// from,
// to: data.email,
// subject: "You've been invited to join an organization",
// react: reactInvitationEmail({
// username: data.email,
// invitedByUsername: data.inviter.user.name,
// invitedByEmail: data.inviter.user.email,
// teamName: data.organization.name,
// inviteLink:
// process.env.NODE_ENV === 'development'
// ? http://localhost:3000/accept-invitation/${data.id}
// : ${ // process.env.BETTER_AUTH_URL || // 'https://demo.better-auth.com' // }/accept-invitation/${data.id},
// }),
// });
// console.log(res, data.email);
},
teams: {
modelName: 'Fund',
enabled: true,
allowRemovingAllTeams: true,
additionalFields: {
isRestricted: {
type: 'boolean',
input: true,
required: true,
},
fundCategory: {
type: 'string',
input: true,
required: true,
},
fundSource: {
type: 'string',
input: true,
required: false,
},
},
},
// organizationLimit: 1,
}),
lastLoginMethod({
storeInDatabase: true,
}),

bearer(),
admin(),
nextCookies(),

],
});
Call await auth.api.setActiveOrganization
try {
console.log(org id ${userOrgs[0].id});
await auth.api.setActiveOrganization({
body: {
organizationId: userOrgs[0].id,
},
// This endpoint requires session cookies.
headers: await headers(),
});
console.log(after setActiveOrganization);
} catch (err: any) {
if (err instanceof APIError) {
const apiErr: APIError = err;
console.log(
error code ${apiErr.statusCode} error message ${apiErr.message},
);
} else {
console.log(other error ${JSON.stringify(err, null, 2)});
}
}

Current vs. Expected behavior

I expect the organizationId to be added to the sesson without error:
Error returned
other error {
"reason": "invalid-input",
"model": "Session"
}

Org id immediately before the call:
org id 7MB5idvLxFQ9UfZVckSrcki1rKxnz6vT

Session object returned by better-auth:
auth.api.getSession {
"session": {
"expiresAt": "2026-04-10T06:27:44.017Z",
"token": "Iwc2ZcaONoYb4XPKnZdITQBzxP5MBi6Z",
"createdAt": "2026-04-03T06:27:44.019Z",
"updatedAt": "2026-04-03T06:27:44.019Z",
"ipAddress": "",
"userAgent": "",
"userId": "q2GfC1TXCOnXuJ1vYA3H5xsrDfB1YSrJ",
"activeTeamId": null,
"impersonatedBy": null,
"id": "aKjWsTWYZr4CIw6kks5AfgWWeTDOg2yV"
},
"user": {
"name": "GenFundUser",
"email": "genfunduser@server.com",
"emailVerified": false,
"image": null,
"createdAt": "2026-03-26T07:03:20.318Z",
"updatedAt": "2026-04-03T06:27:44.063Z",
"lastLoginMethod": "email",
"role": "user",
"banned": false,
"banReason": null,
"banExpires": null,
"displayName": "GenFundUser",
"firstName": "User",
"familyName": "User",
"id": "q2GfC1TXCOnXuJ1vYA3H5xsrDfB1YSrJ"
}
}

What version of Better Auth are you using?

1.5.6

System info

{
  "system": {
    "platform": "darwin",
    "arch": "x64",
    "version": "Darwin Kernel Version 25.4.0: Thu Mar 19 19:27:54 PDT 2026; root:xnu-12377.101.15~1/RELEASE_X86_64",
    "release": "25.4.0",
    "cpuCount": 16,
    "cpuModel": "Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz",
    "totalMemory": "32.00 GB",
    "freeMemory": "0.97 GB"
  },
  "node": {
    "version": "v24.4.1",
    "env": "development"
  },
  "packageManager": {
    "name": "pnpm",
    "version": "10.28.1"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "16.1.6"
    },
    {
      "name": "react",
      "version": "19.2.0"
    }
  ],
  "databases": [
    {
      "name": "pg",
      "version": "^8.20.0"
    },
    {
      "name": "kysely",
      "version": "0.28.8"
    }
  ],
  "betterAuth": {
    "version": "^1.5.6",
    "config": {
      "appName": "Rationes-Charitatis",
      "user": {
        "additionalFields": {
          "role": {
            "type": "string",
            "required": false,
            "defaultValue": "user",
            "input": false
          },
          "displayName": {
            "type": "string",
            "required": true,
            "defaultValue": " ",
            "input": true
          },
          "firstName": {
            "type": "string",
            "required": true,
            "defaultValue": " ",
            "input": true
          },
          "familyName": {
            "type": "string",
            "required": true,
            "defaultValue": " ",
            "input": true
          }
        }
      },
      "emailVerification": {
        "sendOnSignUp": true
      },
      "emailAndPassword": {
        "enabled": true
      },
      "plugins": [
        {
          "name": "organization",
          "config": {
            "id": "organization",
            "endpoints": {},
            "schema": {
              "organization": {
                "fields": {
                  "name": {
                    "type": "string",
                    "required": true,
                    "sortable": true
                  },
                  "slug": {
                    "type": "string",
                    "required": true,
                    "unique": true,
                    "sortable": true,
                    "index": true
                  },
                  "logo": {
                    "type": "string",
                    "required": false
                  },
                  "createdAt": {
                    "type": "date",
                    "required": true
                  },
                  "metadata": {
                    "type": "string",
                    "required": false
                  },
                  "tradingName": {
                    "type": "string",
                    "input": true,
                    "required": true
                  },
                  "legalForm": {
                    "type": "string",
                    "input": true,
                    "required": true
                  },
                  "legalName": {
                    "type": "string",
                    "input": true,
                    "required": true
                  },
                  "charityNumber": {
                    "type": "string",
                    "input": true,
                    "required": false
                  },
                  "taxRef": {
                    "type": "string",
                    "input": true,
                    "required": false
                  },
                  "companyNumber": {
                    "type": "string",
                    "input": true,
                    "required": false
                  },
                  "companyName": {
                    "type": "string",
                    "input": true,
                    "required": false
                  },
                  "idType": {
                    "type": "string",
                    "input": true,
                    "required": true
                  },
                  "identification": {
                    "type": "string",
                    "input": true,
                    "required": true
                  },
                  "trial": {
                    "type": "boolean",
                    "input": true,
                    "required": false
                  },
                  "trialStart": {
                    "type": "date",
                    "input": false,
                    "required": false
                  },
                  "accountType": {
                    "type": "string",
                    "input": true,
                    "required": false
                  }
                }
              },
              "team": {
                "fields": {
                  "name": {
                    "type": "string",
                    "required": true
                  },
                  "organizationId": {
                    "type": "string",
                    "required": true,
                    "references": {
                      "model": "organization",
                      "field": "id"
                    },
                    "index": true
                  },
                  "createdAt": {
                    "type": "date",
                    "required": true
                  },
                  "updatedAt": {
                    "type": "date",
                    "required": false
                  }
                }
              },
              "teamMember": {
                "fields": {
                  "teamId": {
                    "type": "string",
                    "required": true,
                    "references": {
                      "model": "team",
                      "field": "id"
                    },
                    "index": true
                  },
                  "userId": {
                    "type": "string",
                    "required": true,
                    "references": {
                      "model": "user",
                      "field": "id"
                    },
                    "index": true
                  },
                  "createdAt": {
                    "type": "date",
                    "required": false
                  }
                }
              },
              "member": {
                "fields": {
                  "organizationId": {
                    "type": "string",
                    "required": true,
                    "references": {
                      "model": "organization",
                      "field": "id"
                    },
                    "index": true
                  },
                  "userId": {
                    "type": "string",
                    "required": true,
                    "references": {
                      "model": "user",
                      "field": "id"
                    },
                    "index": true
                  },
                  "role": {
                    "type": "string",
                    "required": true,
                    "sortable": true,
                    "defaultValue": "member"
                  },
                  "createdAt": {
                    "type": "date",
                    "required": true
                  }
                }
              },
              "invitation": {
                "fields": {
                  "organizationId": {
                    "type": "string",
                    "required": true,
                    "references": {
                      "model": "organization",
                      "field": "id"
                    },
                    "index": true
                  },
                  "email": {
                    "type": "string",
                    "required": true,
                    "sortable": true,
                    "index": true
                  },
                  "role": {
                    "type": "string",
                    "required": false,
                    "sortable": true
                  },
                  "teamId": {
                    "type": "string",
                    "required": false,
                    "sortable": true
                  },
                  "status": {
                    "type": "string",
                    "required": true,
                    "sortable": true,
                    "defaultValue": "pending"
                  },
                  "expiresAt": {
                    "type": "date",
                    "required": true
                  },
                  "createdAt": {
                    "type": "date",
                    "required": true
                  },
                  "inviterId": {
                    "type": "string",
                    "references": {
                      "model": "user",
                      "field": "id"
                    },
                    "required": true
                  }
                }
              },
              "session": {
                "fields": {
                  "activeOrganizationId": {
                    "type": "string",
                    "required": false
                  },
                  "activeTeamId": {
                    "type": "string",
                    "required": false
                  }
                }
              }
            },
            "$Infer": {
              "Organization": {},
              "Invitation": {},
              "Member": {},
              "Team": {},
              "TeamMember": {},
              "ActiveOrganization": {}
            },
            "$ERROR_CODES": {
              "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_ORGANIZATION": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_ORGANIZATION",
                "message": "You are not allowed to create a new organization"
              },
              "YOU_HAVE_REACHED_THE_MAXIMUM_NUMBER_OF_ORGANIZATIONS": {
                "code": "YOU_HAVE_REACHED_THE_MAXIMUM_NUMBER_OF_ORGANIZATIONS",
                "message": "You have reached the maximum number of organizations"
              },
              "ORGANIZATION_ALREADY_EXISTS": {
                "code": "ORGANIZATION_ALREADY_EXISTS",
                "message": "Organization already exists"
              },
              "ORGANIZATION_SLUG_ALREADY_TAKEN": {
                "code": "ORGANIZATION_SLUG_ALREADY_TAKEN",
                "message": "Organization slug already taken"
              },
              "ORGANIZATION_NOT_FOUND": {
                "code": "ORGANIZATION_NOT_FOUND",
                "message": "Organization not found"
              },
              "USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION": {
                "code": "USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION",
                "message": "User is not a member of the organization"
              },
              "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_ORGANIZATION": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_ORGANIZATION",
                "message": "You are not allowed to update this organization"
              },
              "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_ORGANIZATION": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_ORGANIZATION",
                "message": "You are not allowed to delete this organization"
              },
              "NO_ACTIVE_ORGANIZATION": {
                "code": "NO_ACTIVE_ORGANIZATION",
                "message": "No active organization"
              },
              "USER_IS_ALREADY_A_MEMBER_OF_THIS_ORGANIZATION": {
                "code": "USER_IS_ALREADY_A_MEMBER_OF_THIS_ORGANIZATION",
                "message": "User is already a member of this organization"
              },
              "MEMBER_NOT_FOUND": {
                "code": "MEMBER_NOT_FOUND",
                "message": "Member not found"
              },
              "ROLE_NOT_FOUND": {
                "code": "ROLE_NOT_FOUND",
                "message": "Role not found"
              },
              "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_TEAM": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_TEAM",
                "message": "You are not allowed to create a new team"
              },
              "TEAM_ALREADY_EXISTS": {
                "code": "TEAM_ALREADY_EXISTS",
                "message": "Team already exists"
              },
              "TEAM_NOT_FOUND": {
                "code": "TEAM_NOT_FOUND",
                "message": "Team not found"
              },
              "YOU_CANNOT_LEAVE_THE_ORGANIZATION_AS_THE_ONLY_OWNER": {
                "code": "YOU_CANNOT_LEAVE_THE_ORGANIZATION_AS_THE_ONLY_OWNER",
                "message": "You cannot leave the organization as the only owner"
              },
              "YOU_CANNOT_LEAVE_THE_ORGANIZATION_WITHOUT_AN_OWNER": {
                "code": "YOU_CANNOT_LEAVE_THE_ORGANIZATION_WITHOUT_AN_OWNER",
                "message": "You cannot leave the organization without an owner"
              },
              "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_MEMBER": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_MEMBER",
                "message": "You are not allowed to delete this member"
              },
              "YOU_ARE_NOT_ALLOWED_TO_INVITE_USERS_TO_THIS_ORGANIZATION": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_INVITE_USERS_TO_THIS_ORGANIZATION",
                "message": "You are not allowed to invite users to this organization"
              },
              "USER_IS_ALREADY_INVITED_TO_THIS_ORGANIZATION": {
                "code": "USER_IS_ALREADY_INVITED_TO_THIS_ORGANIZATION",
                "message": "User is already invited to this organization"
              },
              "INVITATION_NOT_FOUND": {
                "code": "INVITATION_NOT_FOUND",
                "message": "Invitation not found"
              },
              "YOU_ARE_NOT_THE_RECIPIENT_OF_THE_INVITATION": {
                "code": "YOU_ARE_NOT_THE_RECIPIENT_OF_THE_INVITATION",
                "message": "You are not the recipient of the invitation"
              },
              "EMAIL_VERIFICATION_REQUIRED_BEFORE_ACCEPTING_OR_REJECTING_INVITATION": {
                "code": "EMAIL_VERIFICATION_REQUIRED_BEFORE_ACCEPTING_OR_REJECTING_INVITATION",
                "message": "Email verification required before accepting or rejecting invitation"
              },
              "YOU_ARE_NOT_ALLOWED_TO_CANCEL_THIS_INVITATION": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_CANCEL_THIS_INVITATION",
                "message": "You are not allowed to cancel this invitation"
              },
              "INVITER_IS_NO_LONGER_A_MEMBER_OF_THE_ORGANIZATION": {
                "code": "INVITER_IS_NO_LONGER_A_MEMBER_OF_THE_ORGANIZATION",
                "message": "Inviter is no longer a member of the organization"
              },
              "YOU_ARE_NOT_ALLOWED_TO_INVITE_USER_WITH_THIS_ROLE": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_INVITE_USER_WITH_THIS_ROLE",
                "message": "You are not allowed to invite a user with this role"
              },
              "FAILED_TO_RETRIEVE_INVITATION": {
                "code": "FAILED_TO_RETRIEVE_INVITATION",
                "message": "Failed to retrieve invitation"
              },
              "YOU_HAVE_REACHED_THE_MAXIMUM_NUMBER_OF_TEAMS": {
                "code": "YOU_HAVE_REACHED_THE_MAXIMUM_NUMBER_OF_TEAMS",
                "message": "You have reached the maximum number of teams"
              },
              "UNABLE_TO_REMOVE_LAST_TEAM": {
                "code": "UNABLE_TO_REMOVE_LAST_TEAM",
                "message": "Unable to remove last team"
              },
              "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_MEMBER": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_MEMBER",
                "message": "You are not allowed to update this member"
              },
              "ORGANIZATION_MEMBERSHIP_LIMIT_REACHED": {
                "code": "ORGANIZATION_MEMBERSHIP_LIMIT_REACHED",
                "message": "Organization membership limit reached"
              },
              "YOU_ARE_NOT_ALLOWED_TO_CREATE_TEAMS_IN_THIS_ORGANIZATION": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_TEAMS_IN_THIS_ORGANIZATION",
                "message": "You are not allowed to create teams in this organization"
              },
              "YOU_ARE_NOT_ALLOWED_TO_DELETE_TEAMS_IN_THIS_ORGANIZATION": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_TEAMS_IN_THIS_ORGANIZATION",
                "message": "You are not allowed to delete teams in this organization"
              },
              "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_TEAM": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_TEAM",
                "message": "You are not allowed to update this team"
              },
              "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_TEAM": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_TEAM",
                "message": "You are not allowed to delete this team"
              },
              "INVITATION_LIMIT_REACHED": {
                "code": "INVITATION_LIMIT_REACHED",
                "message": "Invitation limit reached"
              },
              "TEAM_MEMBER_LIMIT_REACHED": {
                "code": "TEAM_MEMBER_LIMIT_REACHED",
                "message": "Team member limit reached"
              },
              "USER_IS_NOT_A_MEMBER_OF_THE_TEAM": {
                "code": "USER_IS_NOT_A_MEMBER_OF_THE_TEAM",
                "message": "User is not a member of the team"
              },
              "YOU_CAN_NOT_ACCESS_THE_MEMBERS_OF_THIS_TEAM": {
                "code": "YOU_CAN_NOT_ACCESS_THE_MEMBERS_OF_THIS_TEAM",
                "message": "You are not allowed to list the members of this team"
              },
              "YOU_DO_NOT_HAVE_AN_ACTIVE_TEAM": {
                "code": "YOU_DO_NOT_HAVE_AN_ACTIVE_TEAM",
                "message": "You do not have an active team"
              },
              "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_TEAM_MEMBER": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_TEAM_MEMBER",
                "message": "You are not allowed to create a new member"
              },
              "YOU_ARE_NOT_ALLOWED_TO_REMOVE_A_TEAM_MEMBER": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_REMOVE_A_TEAM_MEMBER",
                "message": "You are not allowed to remove a team member"
              },
              "YOU_ARE_NOT_ALLOWED_TO_ACCESS_THIS_ORGANIZATION": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_ACCESS_THIS_ORGANIZATION",
                "message": "You are not allowed to access this organization as an owner"
              },
              "YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION": {
                "code": "YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION",
                "message": "You are not a member of this organization"
              },
              "MISSING_AC_INSTANCE": {
                "code": "MISSING_AC_INSTANCE",
                "message": "Dynamic Access Control requires a pre-defined ac instance on the server auth plugin. Read server logs for more information"
              },
              "YOU_MUST_BE_IN_AN_ORGANIZATION_TO_CREATE_A_ROLE": {
                "code": "YOU_MUST_BE_IN_AN_ORGANIZATION_TO_CREATE_A_ROLE",
                "message": "You must be in an organization to create a role"
              },
              "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_ROLE": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_ROLE",
                "message": "You are not allowed to create a role"
              },
              "YOU_ARE_NOT_ALLOWED_TO_UPDATE_A_ROLE": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_UPDATE_A_ROLE",
                "message": "You are not allowed to update a role"
              },
              "YOU_ARE_NOT_ALLOWED_TO_DELETE_A_ROLE": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_A_ROLE",
                "message": "You are not allowed to delete a role"
              },
              "YOU_ARE_NOT_ALLOWED_TO_READ_A_ROLE": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_READ_A_ROLE",
                "message": "You are not allowed to read a role"
              },
              "YOU_ARE_NOT_ALLOWED_TO_LIST_A_ROLE": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_LIST_A_ROLE",
                "message": "You are not allowed to list a role"
              },
              "YOU_ARE_NOT_ALLOWED_TO_GET_A_ROLE": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_GET_A_ROLE",
                "message": "You are not allowed to get a role"
              },
              "TOO_MANY_ROLES": {
                "code": "TOO_MANY_ROLES",
                "message": "This organization has too many roles"
              },
              "INVALID_RESOURCE": {
                "code": "INVALID_RESOURCE",
                "message": "The provided permission includes an invalid resource"
              },
              "ROLE_NAME_IS_ALREADY_TAKEN": {
                "code": "ROLE_NAME_IS_ALREADY_TAKEN",
                "message": "That role name is already taken"
              },
              "CANNOT_DELETE_A_PRE_DEFINED_ROLE": {
                "code": "CANNOT_DELETE_A_PRE_DEFINED_ROLE",
                "message": "Cannot delete a pre-defined role"
              },
              "ROLE_IS_ASSIGNED_TO_MEMBERS": {
                "code": "ROLE_IS_ASSIGNED_TO_MEMBERS",
                "message": "Cannot delete a role that is assigned to members. Please reassign the members to a different role first"
              }
            },
            "options": {
              "schema": {
                "organization": {
                  "customRoles": [
                    {
                      "role": "orgAdmin",
                      "label": "Organisation Admin"
                    },
                    {
                      "role": "fundAdmin",
                      "label": "Fund Administrator"
                    },
                    {
                      "role": "auditor",
                      "label": "Auditor"
                    },
                    {
                      "role": "salesSuper",
                      "label": "Sales Supervisor"
                    },
                    {
                      "role": "salesInvoice",
                      "label": "Sales invoice"
                    },
                    {
                      "role": "salesCash",
                      "label": "Sales cash"
                    }
                  ],
                  "additionalFields": {
                    "tradingName": {
                      "type": "string",
                      "input": true,
                      "required": true
                    },
                    "legalForm": {
                      "type": "string",
                      "input": true,
                      "required": true
                    },
                    "legalName": {
                      "type": "string",
                      "input": true,
                      "required": true
                    },
                    "charityNumber": {
                      "type": "string",
                      "input": true,
                      "required": false
                    },
                    "taxRef": {
                      "type": "string",
                      "input": true,
                      "required": false
                    },
                    "companyNumber": {
                      "type": "string",
                      "input": true,
                      "required": false
                    },
                    "companyName": {
                      "type": "string",
                      "input": true,
                      "required": false
                    },
                    "idType": {
                      "type": "string",
                      "input": true,
                      "required": true
                    },
                    "identification": {
                      "type": "string",
                      "input": true,
                      "required": true
                    },
                    "trial": {
                      "type": "boolean",
                      "input": true,
                      "required": false
                    },
                    "trialStart": {
                      "type": "date",
                      "input": false,
                      "required": false
                    },
                    "accountType": {
                      "type": "string",
                      "input": true,
                      "required": false
                    }
                  }
                }
              },
              "teams": {
                "modelName": "Fund",
                "enabled": true,
                "allowRemovingAllTeams": true,
                "additionalFields": {
                  "isRestricted": {
                    "type": "boolean",
                    "input": true,
                    "required": true
                  },
                  "fundCategory": {
                    "type": "string",
                    "input": true,
                    "required": true
                  },
                  "fundSource": {
                    "type": "string",
                    "input": true,
                    "required": false
                  }
                }
              }
            }
          }
        },
        {
          "name": "last-login-method",
          "config": {
            "id": "last-login-method",
            "hooks": {
              "after": [
                {}
              ]
            },
            "schema": {
              "user": {
                "fields": {
                  "lastLoginMethod": {
                    "type": "string",
                    "input": false,
                    "required": false,
                    "fieldName": "lastLoginMethod"
                  }
                }
              }
            },
            "options": {
              "storeInDatabase": true
            }
          }
        },
        {
          "name": "bearer",
          "config": {
            "id": "bearer",
            "hooks": {
              "before": [
                {}
              ],
              "after": [
                {}
              ]
            }
          }
        },
        {
          "name": "admin",
          "config": {
            "id": "admin",
            "hooks": {
              "after": [
                {}
              ]
            },
            "endpoints": {},
            "$ERROR_CODES": {
              "FAILED_TO_CREATE_USER": {
                "code": "FAILED_TO_CREATE_USER",
                "message": "Failed to create user"
              },
              "USER_ALREADY_EXISTS": {
                "code": "USER_ALREADY_EXISTS",
                "message": "User already exists."
              },
              "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL": {
                "code": "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL",
                "message": "User already exists. Use another email."
              },
              "YOU_CANNOT_BAN_YOURSELF": {
                "code": "YOU_CANNOT_BAN_YOURSELF",
                "message": "You cannot ban yourself"
              },
              "YOU_ARE_NOT_ALLOWED_TO_CHANGE_USERS_ROLE": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_CHANGE_USERS_ROLE",
                "message": "You are not allowed to change users role"
              },
              "YOU_ARE_NOT_ALLOWED_TO_CREATE_USERS": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_USERS",
                "message": "You are not allowed to create users"
              },
              "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS",
                "message": "You are not allowed to list users"
              },
              "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS_SESSIONS": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS_SESSIONS",
                "message": "You are not allowed to list users sessions"
              },
              "YOU_ARE_NOT_ALLOWED_TO_BAN_USERS": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_BAN_USERS",
                "message": "You are not allowed to ban users"
              },
              "YOU_ARE_NOT_ALLOWED_TO_IMPERSONATE_USERS": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_IMPERSONATE_USERS",
                "message": "You are not allowed to impersonate users"
              },
              "YOU_ARE_NOT_ALLOWED_TO_REVOKE_USERS_SESSIONS": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_REVOKE_USERS_SESSIONS",
                "message": "You are not allowed to revoke users sessions"
              },
              "YOU_ARE_NOT_ALLOWED_TO_DELETE_USERS": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_USERS",
                "message": "You are not allowed to delete users"
              },
              "YOU_ARE_NOT_ALLOWED_TO_SET_USERS_PASSWORD": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_SET_USERS_PASSWORD",
                "message": "You are not allowed to set users password"
              },
              "BANNED_USER": {
                "code": "BANNED_USER",
                "message": "You have been banned from this application"
              },
              "YOU_ARE_NOT_ALLOWED_TO_GET_USER": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_GET_USER",
                "message": "You are not allowed to get user"
              },
              "NO_DATA_TO_UPDATE": {
                "code": "NO_DATA_TO_UPDATE",
                "message": "No data to update"
              },
              "YOU_ARE_NOT_ALLOWED_TO_UPDATE_USERS": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_UPDATE_USERS",
                "message": "You are not allowed to update users"
              },
              "YOU_CANNOT_REMOVE_YOURSELF": {
                "code": "YOU_CANNOT_REMOVE_YOURSELF",
                "message": "You cannot remove yourself"
              },
              "YOU_ARE_NOT_ALLOWED_TO_SET_NON_EXISTENT_VALUE": {
                "code": "YOU_ARE_NOT_ALLOWED_TO_SET_NON_EXISTENT_VALUE",
                "message": "You are not allowed to set a non-existent role value"
              },
              "YOU_CANNOT_IMPERSONATE_ADMINS": {
                "code": "YOU_CANNOT_IMPERSONATE_ADMINS",
                "message": "You cannot impersonate admins"
              },
              "INVALID_ROLE_TYPE": {
                "code": "INVALID_ROLE_TYPE",
                "message": "Invalid role type"
              }
            },
            "schema": {
              "user": {
                "fields": {
                  "role": {
                    "type": "string",
                    "required": false,
                    "input": false
                  },
                  "banned": {
                    "type": "boolean",
                    "defaultValue": false,
                    "required": false,
                    "input": false
                  },
                  "banReason": {
                    "type": "string",
                    "required": false,
                    "input": false
                  },
                  "banExpires": {
                    "type": "date",
                    "required": false,
                    "input": false
                  }
                }
              },
              "session": {
                "fields": {
                  "impersonatedBy": {
                    "type": "string",
                    "required": false
                  }
                }
              }
            }
          }
        },
        {
          "name": "next-cookies",
          "config": {
            "id": "next-cookies",
            "hooks": {
              "before": [
                {}
              ],
              "after": [
                {}
              ]
            }
          }
        }
      ]
    }
  }
}

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

Backend, Client

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

I have tested against the latest release 1.5.6.
It is currently local. If required i can upload to GH.

Originally created by @chrishj59 on GitHub (Apr 3, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8932 Originally assigned to: @ping-maxwell on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### Reproduction Configure auth as: export const auth = betterAuth({ appName: 'Rationes-Charitatis', database: zenstackAdapter(db, { provider: 'postgresql', }), user: { additionalFields: { role: { type: 'string', required: false, defaultValue: 'user', input: false, // don't allow user to set role }, displayName: { type: 'string', required: true, defaultValue: ' ', input: true, }, firstName: { type: 'string', required: true, defaultValue: ' ', input: true, }, familyName: { type: 'string', required: true, defaultValue: ' ', input: true, }, }, }, emailVerification: { // async sendVerificationEmail({ user, url }) { // console.log( // `About to send aws verification email to ${user.email} at url ${url}`, // ); // const resp = await awsVerificationEmail(user.name, user.email, url); // console.log(`AWS resp ${JSON.stringify(resp)}`); // // const res = await resend.emails.send({ // // from, // // to: to || user.email, // // subject: 'Verify your email address', // // html: `<a href="${url}">Verify your email address</a>`, // // }); // // console.log(res, user.email); // }, // sendOnSignUp: true, async sendVerificationEmail({ user, url }) { console.log('Sending verification email to', user.email); const res = await resend.emails.send({ from, to: to || user.email, subject: 'Verify your email address', html: `<a href="${url}">Verify your email address</a>`, }); console.log(res, user.email); }, sendOnSignUp: true, }, emailAndPassword: { enabled: true, async sendResetPassword({ user, url }) { await resend.emails.send({ from, to: user.email, subject: 'Reset your password', react: reactResetPasswordEmail({ username: user.email, resetLink: url, }), }); }, }, plugins: [ organization({ schema: { organization: { // name: 'Trading name', customRoles: [ { role: 'orgAdmin', label: 'Organisation Admin' }, { role: 'fundAdmin', label: 'Fund Administrator' }, { role: 'auditor', label: 'Auditor' }, { role: 'salesSuper', label: 'Sales Supervisor' }, { role: 'salesInvoice', label: 'Sales invoice' }, { role: 'salesCash', label: 'Sales cash' }, ], additionalFields: { tradingName: { type: 'string', input: true, required: true, }, legalForm: { type: 'string', input: true, required: true, }, legalName: { type: 'string', input: true, required: true, }, charityNumber: { type: 'string', input: true, required: false, }, taxRef: { type: 'string', input: true, required: false, }, companyNumber: { type: 'string', input: true, required: false, }, companyName: { type: 'string', input: true, required: false, }, idType: { type: 'string', input: true, required: true, }, identification: { type: 'string', input: true, required: true, }, trial: { type: 'boolean', input: true, required: false, }, trialStart: { type: 'date', input: false, required: false, }, accountType: { type: 'string', input: true, required: false, }, }, }, }, async sendInvitationEmail() { // const res = await resend.emails.send({ // from, // to: data.email, // subject: "You've been invited to join an organization", // react: reactInvitationEmail({ // username: data.email, // invitedByUsername: data.inviter.user.name, // invitedByEmail: data.inviter.user.email, // teamName: data.organization.name, // inviteLink: // process.env.NODE_ENV === 'development' // ? `http://localhost:3000/accept-invitation/${data.id}` // : `${ // process.env.BETTER_AUTH_URL || // 'https://demo.better-auth.com' // }/accept-invitation/${data.id}`, // }), // }); // console.log(res, data.email); }, teams: { modelName: 'Fund', enabled: true, allowRemovingAllTeams: true, additionalFields: { isRestricted: { type: 'boolean', input: true, required: true, }, fundCategory: { type: 'string', input: true, required: true, }, fundSource: { type: 'string', input: true, required: false, }, }, }, // organizationLimit: 1, }), lastLoginMethod({ storeInDatabase: true, }), bearer(), admin(), nextCookies(), ], }); Call await auth.api.setActiveOrganization try { console.log(`org id ${userOrgs[0].id}`); await auth.api.setActiveOrganization({ body: { organizationId: userOrgs[0].id, }, // This endpoint requires session cookies. headers: await headers(), }); console.log(`after setActiveOrganization`); } catch (err: any) { if (err instanceof APIError) { const apiErr: APIError = err; console.log( `error code ${apiErr.statusCode} error message ${apiErr.message}`, ); } else { console.log(`other error ${JSON.stringify(err, null, 2)}`); } } ### Current vs. Expected behavior I expect the organizationId to be added to the sesson without error: Error returned other error { "reason": "invalid-input", "model": "Session" } Org id immediately before the call: org id 7MB5idvLxFQ9UfZVckSrcki1rKxnz6vT Session object returned by better-auth: auth.api.getSession { "session": { "expiresAt": "2026-04-10T06:27:44.017Z", "token": "Iwc2ZcaONoYb4XPKnZdITQBzxP5MBi6Z", "createdAt": "2026-04-03T06:27:44.019Z", "updatedAt": "2026-04-03T06:27:44.019Z", "ipAddress": "", "userAgent": "", "userId": "q2GfC1TXCOnXuJ1vYA3H5xsrDfB1YSrJ", "activeTeamId": null, "impersonatedBy": null, "id": "aKjWsTWYZr4CIw6kks5AfgWWeTDOg2yV" }, "user": { "name": "GenFundUser", "email": "genfunduser@server.com", "emailVerified": false, "image": null, "createdAt": "2026-03-26T07:03:20.318Z", "updatedAt": "2026-04-03T06:27:44.063Z", "lastLoginMethod": "email", "role": "user", "banned": false, "banReason": null, "banExpires": null, "displayName": "GenFundUser", "firstName": "User", "familyName": "User", "id": "q2GfC1TXCOnXuJ1vYA3H5xsrDfB1YSrJ" } } ### What version of Better Auth are you using? 1.5.6 ### System info ```bash { "system": { "platform": "darwin", "arch": "x64", "version": "Darwin Kernel Version 25.4.0: Thu Mar 19 19:27:54 PDT 2026; root:xnu-12377.101.15~1/RELEASE_X86_64", "release": "25.4.0", "cpuCount": 16, "cpuModel": "Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz", "totalMemory": "32.00 GB", "freeMemory": "0.97 GB" }, "node": { "version": "v24.4.1", "env": "development" }, "packageManager": { "name": "pnpm", "version": "10.28.1" }, "frameworks": [ { "name": "next", "version": "16.1.6" }, { "name": "react", "version": "19.2.0" } ], "databases": [ { "name": "pg", "version": "^8.20.0" }, { "name": "kysely", "version": "0.28.8" } ], "betterAuth": { "version": "^1.5.6", "config": { "appName": "Rationes-Charitatis", "user": { "additionalFields": { "role": { "type": "string", "required": false, "defaultValue": "user", "input": false }, "displayName": { "type": "string", "required": true, "defaultValue": " ", "input": true }, "firstName": { "type": "string", "required": true, "defaultValue": " ", "input": true }, "familyName": { "type": "string", "required": true, "defaultValue": " ", "input": true } } }, "emailVerification": { "sendOnSignUp": true }, "emailAndPassword": { "enabled": true }, "plugins": [ { "name": "organization", "config": { "id": "organization", "endpoints": {}, "schema": { "organization": { "fields": { "name": { "type": "string", "required": true, "sortable": true }, "slug": { "type": "string", "required": true, "unique": true, "sortable": true, "index": true }, "logo": { "type": "string", "required": false }, "createdAt": { "type": "date", "required": true }, "metadata": { "type": "string", "required": false }, "tradingName": { "type": "string", "input": true, "required": true }, "legalForm": { "type": "string", "input": true, "required": true }, "legalName": { "type": "string", "input": true, "required": true }, "charityNumber": { "type": "string", "input": true, "required": false }, "taxRef": { "type": "string", "input": true, "required": false }, "companyNumber": { "type": "string", "input": true, "required": false }, "companyName": { "type": "string", "input": true, "required": false }, "idType": { "type": "string", "input": true, "required": true }, "identification": { "type": "string", "input": true, "required": true }, "trial": { "type": "boolean", "input": true, "required": false }, "trialStart": { "type": "date", "input": false, "required": false }, "accountType": { "type": "string", "input": true, "required": false } } }, "team": { "fields": { "name": { "type": "string", "required": true }, "organizationId": { "type": "string", "required": true, "references": { "model": "organization", "field": "id" }, "index": true }, "createdAt": { "type": "date", "required": true }, "updatedAt": { "type": "date", "required": false } } }, "teamMember": { "fields": { "teamId": { "type": "string", "required": true, "references": { "model": "team", "field": "id" }, "index": true }, "userId": { "type": "string", "required": true, "references": { "model": "user", "field": "id" }, "index": true }, "createdAt": { "type": "date", "required": false } } }, "member": { "fields": { "organizationId": { "type": "string", "required": true, "references": { "model": "organization", "field": "id" }, "index": true }, "userId": { "type": "string", "required": true, "references": { "model": "user", "field": "id" }, "index": true }, "role": { "type": "string", "required": true, "sortable": true, "defaultValue": "member" }, "createdAt": { "type": "date", "required": true } } }, "invitation": { "fields": { "organizationId": { "type": "string", "required": true, "references": { "model": "organization", "field": "id" }, "index": true }, "email": { "type": "string", "required": true, "sortable": true, "index": true }, "role": { "type": "string", "required": false, "sortable": true }, "teamId": { "type": "string", "required": false, "sortable": true }, "status": { "type": "string", "required": true, "sortable": true, "defaultValue": "pending" }, "expiresAt": { "type": "date", "required": true }, "createdAt": { "type": "date", "required": true }, "inviterId": { "type": "string", "references": { "model": "user", "field": "id" }, "required": true } } }, "session": { "fields": { "activeOrganizationId": { "type": "string", "required": false }, "activeTeamId": { "type": "string", "required": false } } } }, "$Infer": { "Organization": {}, "Invitation": {}, "Member": {}, "Team": {}, "TeamMember": {}, "ActiveOrganization": {} }, "$ERROR_CODES": { "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_ORGANIZATION": { "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_ORGANIZATION", "message": "You are not allowed to create a new organization" }, "YOU_HAVE_REACHED_THE_MAXIMUM_NUMBER_OF_ORGANIZATIONS": { "code": "YOU_HAVE_REACHED_THE_MAXIMUM_NUMBER_OF_ORGANIZATIONS", "message": "You have reached the maximum number of organizations" }, "ORGANIZATION_ALREADY_EXISTS": { "code": "ORGANIZATION_ALREADY_EXISTS", "message": "Organization already exists" }, "ORGANIZATION_SLUG_ALREADY_TAKEN": { "code": "ORGANIZATION_SLUG_ALREADY_TAKEN", "message": "Organization slug already taken" }, "ORGANIZATION_NOT_FOUND": { "code": "ORGANIZATION_NOT_FOUND", "message": "Organization not found" }, "USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION": { "code": "USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION", "message": "User is not a member of the organization" }, "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_ORGANIZATION": { "code": "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_ORGANIZATION", "message": "You are not allowed to update this organization" }, "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_ORGANIZATION": { "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_ORGANIZATION", "message": "You are not allowed to delete this organization" }, "NO_ACTIVE_ORGANIZATION": { "code": "NO_ACTIVE_ORGANIZATION", "message": "No active organization" }, "USER_IS_ALREADY_A_MEMBER_OF_THIS_ORGANIZATION": { "code": "USER_IS_ALREADY_A_MEMBER_OF_THIS_ORGANIZATION", "message": "User is already a member of this organization" }, "MEMBER_NOT_FOUND": { "code": "MEMBER_NOT_FOUND", "message": "Member not found" }, "ROLE_NOT_FOUND": { "code": "ROLE_NOT_FOUND", "message": "Role not found" }, "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_TEAM": { "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_TEAM", "message": "You are not allowed to create a new team" }, "TEAM_ALREADY_EXISTS": { "code": "TEAM_ALREADY_EXISTS", "message": "Team already exists" }, "TEAM_NOT_FOUND": { "code": "TEAM_NOT_FOUND", "message": "Team not found" }, "YOU_CANNOT_LEAVE_THE_ORGANIZATION_AS_THE_ONLY_OWNER": { "code": "YOU_CANNOT_LEAVE_THE_ORGANIZATION_AS_THE_ONLY_OWNER", "message": "You cannot leave the organization as the only owner" }, "YOU_CANNOT_LEAVE_THE_ORGANIZATION_WITHOUT_AN_OWNER": { "code": "YOU_CANNOT_LEAVE_THE_ORGANIZATION_WITHOUT_AN_OWNER", "message": "You cannot leave the organization without an owner" }, "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_MEMBER": { "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_MEMBER", "message": "You are not allowed to delete this member" }, "YOU_ARE_NOT_ALLOWED_TO_INVITE_USERS_TO_THIS_ORGANIZATION": { "code": "YOU_ARE_NOT_ALLOWED_TO_INVITE_USERS_TO_THIS_ORGANIZATION", "message": "You are not allowed to invite users to this organization" }, "USER_IS_ALREADY_INVITED_TO_THIS_ORGANIZATION": { "code": "USER_IS_ALREADY_INVITED_TO_THIS_ORGANIZATION", "message": "User is already invited to this organization" }, "INVITATION_NOT_FOUND": { "code": "INVITATION_NOT_FOUND", "message": "Invitation not found" }, "YOU_ARE_NOT_THE_RECIPIENT_OF_THE_INVITATION": { "code": "YOU_ARE_NOT_THE_RECIPIENT_OF_THE_INVITATION", "message": "You are not the recipient of the invitation" }, "EMAIL_VERIFICATION_REQUIRED_BEFORE_ACCEPTING_OR_REJECTING_INVITATION": { "code": "EMAIL_VERIFICATION_REQUIRED_BEFORE_ACCEPTING_OR_REJECTING_INVITATION", "message": "Email verification required before accepting or rejecting invitation" }, "YOU_ARE_NOT_ALLOWED_TO_CANCEL_THIS_INVITATION": { "code": "YOU_ARE_NOT_ALLOWED_TO_CANCEL_THIS_INVITATION", "message": "You are not allowed to cancel this invitation" }, "INVITER_IS_NO_LONGER_A_MEMBER_OF_THE_ORGANIZATION": { "code": "INVITER_IS_NO_LONGER_A_MEMBER_OF_THE_ORGANIZATION", "message": "Inviter is no longer a member of the organization" }, "YOU_ARE_NOT_ALLOWED_TO_INVITE_USER_WITH_THIS_ROLE": { "code": "YOU_ARE_NOT_ALLOWED_TO_INVITE_USER_WITH_THIS_ROLE", "message": "You are not allowed to invite a user with this role" }, "FAILED_TO_RETRIEVE_INVITATION": { "code": "FAILED_TO_RETRIEVE_INVITATION", "message": "Failed to retrieve invitation" }, "YOU_HAVE_REACHED_THE_MAXIMUM_NUMBER_OF_TEAMS": { "code": "YOU_HAVE_REACHED_THE_MAXIMUM_NUMBER_OF_TEAMS", "message": "You have reached the maximum number of teams" }, "UNABLE_TO_REMOVE_LAST_TEAM": { "code": "UNABLE_TO_REMOVE_LAST_TEAM", "message": "Unable to remove last team" }, "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_MEMBER": { "code": "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_MEMBER", "message": "You are not allowed to update this member" }, "ORGANIZATION_MEMBERSHIP_LIMIT_REACHED": { "code": "ORGANIZATION_MEMBERSHIP_LIMIT_REACHED", "message": "Organization membership limit reached" }, "YOU_ARE_NOT_ALLOWED_TO_CREATE_TEAMS_IN_THIS_ORGANIZATION": { "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_TEAMS_IN_THIS_ORGANIZATION", "message": "You are not allowed to create teams in this organization" }, "YOU_ARE_NOT_ALLOWED_TO_DELETE_TEAMS_IN_THIS_ORGANIZATION": { "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_TEAMS_IN_THIS_ORGANIZATION", "message": "You are not allowed to delete teams in this organization" }, "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_TEAM": { "code": "YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_TEAM", "message": "You are not allowed to update this team" }, "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_TEAM": { "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_TEAM", "message": "You are not allowed to delete this team" }, "INVITATION_LIMIT_REACHED": { "code": "INVITATION_LIMIT_REACHED", "message": "Invitation limit reached" }, "TEAM_MEMBER_LIMIT_REACHED": { "code": "TEAM_MEMBER_LIMIT_REACHED", "message": "Team member limit reached" }, "USER_IS_NOT_A_MEMBER_OF_THE_TEAM": { "code": "USER_IS_NOT_A_MEMBER_OF_THE_TEAM", "message": "User is not a member of the team" }, "YOU_CAN_NOT_ACCESS_THE_MEMBERS_OF_THIS_TEAM": { "code": "YOU_CAN_NOT_ACCESS_THE_MEMBERS_OF_THIS_TEAM", "message": "You are not allowed to list the members of this team" }, "YOU_DO_NOT_HAVE_AN_ACTIVE_TEAM": { "code": "YOU_DO_NOT_HAVE_AN_ACTIVE_TEAM", "message": "You do not have an active team" }, "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_TEAM_MEMBER": { "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_TEAM_MEMBER", "message": "You are not allowed to create a new member" }, "YOU_ARE_NOT_ALLOWED_TO_REMOVE_A_TEAM_MEMBER": { "code": "YOU_ARE_NOT_ALLOWED_TO_REMOVE_A_TEAM_MEMBER", "message": "You are not allowed to remove a team member" }, "YOU_ARE_NOT_ALLOWED_TO_ACCESS_THIS_ORGANIZATION": { "code": "YOU_ARE_NOT_ALLOWED_TO_ACCESS_THIS_ORGANIZATION", "message": "You are not allowed to access this organization as an owner" }, "YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION": { "code": "YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION", "message": "You are not a member of this organization" }, "MISSING_AC_INSTANCE": { "code": "MISSING_AC_INSTANCE", "message": "Dynamic Access Control requires a pre-defined ac instance on the server auth plugin. Read server logs for more information" }, "YOU_MUST_BE_IN_AN_ORGANIZATION_TO_CREATE_A_ROLE": { "code": "YOU_MUST_BE_IN_AN_ORGANIZATION_TO_CREATE_A_ROLE", "message": "You must be in an organization to create a role" }, "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_ROLE": { "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_A_ROLE", "message": "You are not allowed to create a role" }, "YOU_ARE_NOT_ALLOWED_TO_UPDATE_A_ROLE": { "code": "YOU_ARE_NOT_ALLOWED_TO_UPDATE_A_ROLE", "message": "You are not allowed to update a role" }, "YOU_ARE_NOT_ALLOWED_TO_DELETE_A_ROLE": { "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_A_ROLE", "message": "You are not allowed to delete a role" }, "YOU_ARE_NOT_ALLOWED_TO_READ_A_ROLE": { "code": "YOU_ARE_NOT_ALLOWED_TO_READ_A_ROLE", "message": "You are not allowed to read a role" }, "YOU_ARE_NOT_ALLOWED_TO_LIST_A_ROLE": { "code": "YOU_ARE_NOT_ALLOWED_TO_LIST_A_ROLE", "message": "You are not allowed to list a role" }, "YOU_ARE_NOT_ALLOWED_TO_GET_A_ROLE": { "code": "YOU_ARE_NOT_ALLOWED_TO_GET_A_ROLE", "message": "You are not allowed to get a role" }, "TOO_MANY_ROLES": { "code": "TOO_MANY_ROLES", "message": "This organization has too many roles" }, "INVALID_RESOURCE": { "code": "INVALID_RESOURCE", "message": "The provided permission includes an invalid resource" }, "ROLE_NAME_IS_ALREADY_TAKEN": { "code": "ROLE_NAME_IS_ALREADY_TAKEN", "message": "That role name is already taken" }, "CANNOT_DELETE_A_PRE_DEFINED_ROLE": { "code": "CANNOT_DELETE_A_PRE_DEFINED_ROLE", "message": "Cannot delete a pre-defined role" }, "ROLE_IS_ASSIGNED_TO_MEMBERS": { "code": "ROLE_IS_ASSIGNED_TO_MEMBERS", "message": "Cannot delete a role that is assigned to members. Please reassign the members to a different role first" } }, "options": { "schema": { "organization": { "customRoles": [ { "role": "orgAdmin", "label": "Organisation Admin" }, { "role": "fundAdmin", "label": "Fund Administrator" }, { "role": "auditor", "label": "Auditor" }, { "role": "salesSuper", "label": "Sales Supervisor" }, { "role": "salesInvoice", "label": "Sales invoice" }, { "role": "salesCash", "label": "Sales cash" } ], "additionalFields": { "tradingName": { "type": "string", "input": true, "required": true }, "legalForm": { "type": "string", "input": true, "required": true }, "legalName": { "type": "string", "input": true, "required": true }, "charityNumber": { "type": "string", "input": true, "required": false }, "taxRef": { "type": "string", "input": true, "required": false }, "companyNumber": { "type": "string", "input": true, "required": false }, "companyName": { "type": "string", "input": true, "required": false }, "idType": { "type": "string", "input": true, "required": true }, "identification": { "type": "string", "input": true, "required": true }, "trial": { "type": "boolean", "input": true, "required": false }, "trialStart": { "type": "date", "input": false, "required": false }, "accountType": { "type": "string", "input": true, "required": false } } } }, "teams": { "modelName": "Fund", "enabled": true, "allowRemovingAllTeams": true, "additionalFields": { "isRestricted": { "type": "boolean", "input": true, "required": true }, "fundCategory": { "type": "string", "input": true, "required": true }, "fundSource": { "type": "string", "input": true, "required": false } } } } } }, { "name": "last-login-method", "config": { "id": "last-login-method", "hooks": { "after": [ {} ] }, "schema": { "user": { "fields": { "lastLoginMethod": { "type": "string", "input": false, "required": false, "fieldName": "lastLoginMethod" } } } }, "options": { "storeInDatabase": true } } }, { "name": "bearer", "config": { "id": "bearer", "hooks": { "before": [ {} ], "after": [ {} ] } } }, { "name": "admin", "config": { "id": "admin", "hooks": { "after": [ {} ] }, "endpoints": {}, "$ERROR_CODES": { "FAILED_TO_CREATE_USER": { "code": "FAILED_TO_CREATE_USER", "message": "Failed to create user" }, "USER_ALREADY_EXISTS": { "code": "USER_ALREADY_EXISTS", "message": "User already exists." }, "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL": { "code": "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL", "message": "User already exists. Use another email." }, "YOU_CANNOT_BAN_YOURSELF": { "code": "YOU_CANNOT_BAN_YOURSELF", "message": "You cannot ban yourself" }, "YOU_ARE_NOT_ALLOWED_TO_CHANGE_USERS_ROLE": { "code": "YOU_ARE_NOT_ALLOWED_TO_CHANGE_USERS_ROLE", "message": "You are not allowed to change users role" }, "YOU_ARE_NOT_ALLOWED_TO_CREATE_USERS": { "code": "YOU_ARE_NOT_ALLOWED_TO_CREATE_USERS", "message": "You are not allowed to create users" }, "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS": { "code": "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS", "message": "You are not allowed to list users" }, "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS_SESSIONS": { "code": "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS_SESSIONS", "message": "You are not allowed to list users sessions" }, "YOU_ARE_NOT_ALLOWED_TO_BAN_USERS": { "code": "YOU_ARE_NOT_ALLOWED_TO_BAN_USERS", "message": "You are not allowed to ban users" }, "YOU_ARE_NOT_ALLOWED_TO_IMPERSONATE_USERS": { "code": "YOU_ARE_NOT_ALLOWED_TO_IMPERSONATE_USERS", "message": "You are not allowed to impersonate users" }, "YOU_ARE_NOT_ALLOWED_TO_REVOKE_USERS_SESSIONS": { "code": "YOU_ARE_NOT_ALLOWED_TO_REVOKE_USERS_SESSIONS", "message": "You are not allowed to revoke users sessions" }, "YOU_ARE_NOT_ALLOWED_TO_DELETE_USERS": { "code": "YOU_ARE_NOT_ALLOWED_TO_DELETE_USERS", "message": "You are not allowed to delete users" }, "YOU_ARE_NOT_ALLOWED_TO_SET_USERS_PASSWORD": { "code": "YOU_ARE_NOT_ALLOWED_TO_SET_USERS_PASSWORD", "message": "You are not allowed to set users password" }, "BANNED_USER": { "code": "BANNED_USER", "message": "You have been banned from this application" }, "YOU_ARE_NOT_ALLOWED_TO_GET_USER": { "code": "YOU_ARE_NOT_ALLOWED_TO_GET_USER", "message": "You are not allowed to get user" }, "NO_DATA_TO_UPDATE": { "code": "NO_DATA_TO_UPDATE", "message": "No data to update" }, "YOU_ARE_NOT_ALLOWED_TO_UPDATE_USERS": { "code": "YOU_ARE_NOT_ALLOWED_TO_UPDATE_USERS", "message": "You are not allowed to update users" }, "YOU_CANNOT_REMOVE_YOURSELF": { "code": "YOU_CANNOT_REMOVE_YOURSELF", "message": "You cannot remove yourself" }, "YOU_ARE_NOT_ALLOWED_TO_SET_NON_EXISTENT_VALUE": { "code": "YOU_ARE_NOT_ALLOWED_TO_SET_NON_EXISTENT_VALUE", "message": "You are not allowed to set a non-existent role value" }, "YOU_CANNOT_IMPERSONATE_ADMINS": { "code": "YOU_CANNOT_IMPERSONATE_ADMINS", "message": "You cannot impersonate admins" }, "INVALID_ROLE_TYPE": { "code": "INVALID_ROLE_TYPE", "message": "Invalid role type" } }, "schema": { "user": { "fields": { "role": { "type": "string", "required": false, "input": false }, "banned": { "type": "boolean", "defaultValue": false, "required": false, "input": false }, "banReason": { "type": "string", "required": false, "input": false }, "banExpires": { "type": "date", "required": false, "input": false } } }, "session": { "fields": { "impersonatedBy": { "type": "string", "required": false } } } } } }, { "name": "next-cookies", "config": { "id": "next-cookies", "hooks": { "before": [ {} ], "after": [ {} ] } } } ] } } } ``` ### Which area(s) are affected? (Select all that apply) Backend, Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context I have tested against the latest release 1.5.6. It is currently local. If required i can upload to GH.
GiteaMirror added the bug label 2026-04-13 07:35:12 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Apr 3, 2026):

Hi @chrishj59! I'm Dosu and I’m helping the better-auth team.

The error {"reason": "invalid-input", "model": "Session"} is almost certainly coming from your ZenStack adapter, not from Better Auth itself — this error format doesn't exist in Better Auth's codebase [1].

Here's what's happening: when setActiveOrganization is called, it tries to update the session record with activeOrganizationId [2]. The organization plugin adds this field to Better Auth's internal session schema [3], but ZenStack requires you to explicitly define all fields in your .zmodel file. If activeOrganizationId isn't in your ZenStack Session model, the adapter rejects the update.

A strong clue: your session object shows activeTeamId: null but activeOrganizationId is completely absent, suggesting that column doesn't exist in your ZenStack schema/database.

Fix: Add the missing fields to your ZenStack Session model:

model Session {
  // ... your existing fields ...
  activeOrganizationId   String?  // Add this
  activeTeamId           String?  // Add this if not already present
  impersonatedBy         String?  // From admin plugin
  // ...
}

Then regenerate and migrate:

npx zenstack generate
# Run your database migration as needed

This is a common issue with custom/community adapters — they require manual schema synchronization when Better Auth plugins add new fields [4]. The built-in adapters (Prisma, Drizzle, Kysely) handle this more automatically.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other
Chat with Dosu Open in Cursor Join Discord Share on X

<!-- gh-comment-id:4183566751 --> @dosubot[bot] commented on GitHub (Apr 3, 2026): <!-- Greeting --> Hi @chrishj59! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> The error `{"reason": "invalid-input", "model": "Session"}` is almost certainly coming from **your ZenStack adapter**, not from Better Auth itself — this error format doesn't exist in Better Auth's codebase [[1]](https://github.com/better-auth/better-auth/blob/77ead28455cb41f403adcb7b67d0d9fc4fa78286/packages/better-auth/src/db/schema.ts#L118-L135). Here's what's happening: when `setActiveOrganization` is called, it tries to update the session record with `activeOrganizationId` [[2]](https://github.com/better-auth/better-auth/blob/77ead28455cb41f403adcb7b67d0d9fc4fa78286/packages/better-auth/src/plugins/organization/adapter.ts#L440-L452). The organization plugin adds this field to Better Auth's internal session schema [[3]](https://github.com/better-auth/better-auth/blob/77ead28455cb41f403adcb7b67d0d9fc4fa78286/packages/better-auth/src/plugins/organization/organization.ts#L1220-L1226), but **ZenStack requires you to explicitly define all fields in your `.zmodel` file**. If `activeOrganizationId` isn't in your ZenStack Session model, the adapter rejects the update. A strong clue: your session object shows `activeTeamId: null` but **`activeOrganizationId` is completely absent**, suggesting that column doesn't exist in your ZenStack schema/database. **Fix**: Add the missing fields to your ZenStack Session model: ```zmodel model Session { // ... your existing fields ... activeOrganizationId String? // Add this activeTeamId String? // Add this if not already present impersonatedBy String? // From admin plugin // ... } ``` Then regenerate and migrate: ```bash npx zenstack generate # Run your database migration as needed ``` This is a common issue with custom/community adapters — they require manual schema synchronization when Better Auth plugins add new fields [[4]](https://github.com/better-auth/better-auth/issues/2405#issuecomment-2869922426). The built-in adapters (Prisma, Drizzle, Kysely) handle this more automatically. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=brand-link).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-great_response#message_id=53e8f793-32fe-4523-b4d3-b66947f2c5d1&feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-irrelevant_answer#message_id=53e8f793-32fe-4523-b4d3-b66947f2c5d1&feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-incorrect_sources#message_id=53e8f793-32fe-4523-b4d3-b66947f2c5d1&feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-too_verbose#message_id=53e8f793-32fe-4523-b4d3-b66947f2c5d1&feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-hallucination#message_id=53e8f793-32fe-4523-b4d3-b66947f2c5d1&feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-bug_report#message_id=53e8f793-32fe-4523-b4d3-b66947f2c5d1&feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-other#message_id=53e8f793-32fe-4523-b4d3-b66947f2c5d1&feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=chat-badge)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=The%20error%20%60%7B%22reason%22%3A%20%22invalid-input%22%2C%20%22model%22%3A%20%22Session%22%7D%60%20is%20almost%20certainly%20coming%20from%20%2A%2Ayour%20ZenStack%20adapter%2A%2A%2C%20not%20from%20Better%20Auth%20itself%20%E2%80%94%20this%20error%20format%20doesn%27t%20exist%20in%20Better%20Auth%27s%20codebase%20%5B%5B1%5D%5D%28https%3A//github.com/better-auth/better-auth/blob/77ead28455cb41f403adcb7b67d0d9fc4fa78286/packages/better-auth/src/db/schema.ts%23L118-L135%29.%0A%0AHere%27s%20what%27s%20happening%3A%20when%20%60setActiveOrganization%60%20is%20called%2C%20it%20tries%20to%20update%20the%20session%20record%20with%20%60activeOrganizationId%60%20%5B%5B2%5D%5D%28https%3A//github.com/better-auth/better-auth/blob/77ead28455cb41f403adcb7b67d0d9fc4fa78286/packages/better-auth/src/plugins/organization/adapter.ts%23L440-L452%29.%20The%20organization%20plugin%20adds%20this%20field%20to%20Better%20Auth%27s%20internal%20session%20schema%20%5B%5B3%5D%5D%28https%3A//github.com/better-auth/better-auth/blob/77ead28455cb41f403adcb7b67d0d9fc4fa78286/packages/better-auth/src/plugins/organization/organization.ts%23L1220-L1226%29%2C%20but%20%2A%2AZenStack%20requires%20you%20to%20explicitly%20define%20all%20fields%20in%20your%20%60.zmodel%60%20file%2A%2A.%20If%20%60activeOrganizationId%60%20isn%27t%20in%20your%20ZenStack%20Session%20model%2C%20the%20adapter%20rejects%20the%20update.%0A%0AA%20strong%20clue%3A%20your%20session%20object%20shows%20%60activeTeamId%3A%20null%60%20but%20%2A%2A%60activeOrganizationId%60%20is%20completely%20absent%2A%2A%2C%20suggesting%20that%20column%20doesn%27t%20exist%20in%20your%20ZenStack%20schema/database.%0A%0A%2A%2AFix%2A%2A%3A%20Add%20the%20missing%20fields%20to%20your%20ZenStack%20Session%20model%3A%0A%0A%60%60%60zmodel%0Amodel%20Session%20%7B%0A%20%20//%20...%20your%20existing%20fields%20...%0A%20%20activeOrganizationId%20%20%20String%3F%20%20//%20Add%20this%0A%20%20activeTeamId%20%20%20%20%20%20%20%20%20%20%20String%3F%20%20//%20Add%20this%20if%20not%20already%20present%0A%20%20impersonatedBy%20%20%20%20%20%20%20%20%20String%3F%20%20//%20From%20admin%20plugin%0A%20%20//%20...%0A%7D%0A%60%60%60%0A%0AThen%20regenerate%20and%20migrate%3A%0A%0A%60%60%60bash%0Anpx%20zenstack%20generate%0A%23%20Run%20your%20database%20migration%20as%20needed%0A%60%60%60%0A%0AThis%20is%20a%20common%20issue%20with%20custom/community%20adapters%20%E2%80%94%20they%20require%20manual%20schema%20synchronization%20when%20Better%20Auth%20plugins%20add%20new%20fields%20%5B%5B4%5D%5D%28https%3A//github.com/better-auth/better-auth/issues/2405%23issuecomment-2869922426%29.%20The%20built-in%20adapters%20%28Prisma%2C%20Drizzle%2C%20Kysely%29%20handle%20this%20more%20automatically.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=join-discord)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/8932)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#11241