Adding Members to Organization with Teams Enabled

I am using the organization plugin with teams enabled and am trying to add members to the organization/team via the addMember API call:
await auth.api.addMember({
body: {
organizationId: <org_id>,
userId: <user_id>,
role: "admin",
teamId: <team_id>, // this is not working
},
});
await auth.api.addMember({
body: {
organizationId: <org_id>,
userId: <user_id>,
role: "admin",
teamId: <team_id>, // this is not working
},
});
I am trying this but when I inspect my database table, I do not see the teamId being recorded into the database column. What am I missing?
12 Replies
Netrifier
Netrifier3d ago
It seems the addMember function currently doesn't include the ability to specify a teamId. Hey @Ping , I'd be glad to make a pr to add teamId to addMember. Would that be okay?
Ping
Ping3d ago
cc @bekacru
bekacru
bekacru3d ago
yeah go for it
Netrifier
Netrifier3d ago
Hey, I modified the findTeamById function in the org adapter to take in additional arguments. Does this look good to you, or would it be better to revert back to the original?
findTeamById: async <IncludeMembers extends boolean>({
teamId,
organizationId,
includeTeamMembers,
}: {
teamId: string;
organizationId?: string;
// Only return members if required
includeTeamMembers?: IncludeMembers;
}): Promise<
(Team & (IncludeMembers extends true ? { members: Member[] } : {})) | null
> => {
findTeamById: async <IncludeMembers extends boolean>({
teamId,
organizationId,
includeTeamMembers,
}: {
teamId: string;
organizationId?: string;
// Only return members if required
includeTeamMembers?: IncludeMembers;
}): Promise<
(Team & (IncludeMembers extends true ? { members: Member[] } : {})) | null
> => {
Netrifier
Netrifier3d ago
pr is ready for review, Let me know if the adapter method change looks good or if it should be reverted.
GitHub
feat(organization): Add teamId in addMember by Netrifier · Pull Req...
Added teamId option to addMember await auth.api.addMember({ body: { organizationId: &lt;org_id&gt;, userId: &lt;user_id&gt;, role: &quot;admin&quot;, ...
MaveriX89
MaveriX89OP3d ago
Thank you for the PR!
Sanjay Kholiya
is this member table schema from docs correct? If we are adding a member to a team, don't we need to specify the teamId in the member table as well?
No description
Netrifier
Netrifier2d ago
yes, you need to add teamId in the member and invitations schema
Sanjay Kholiya
Cool, thanks. I had one more query, If I create a team inside an org, will org members be able to see all the teams or just the teams that they are a member of? I want that team should ONLY be visible to the members of that team. Other org members should not be able to see the teams they are NOT a part of. Is this possible with current implementation?
Netrifier
Netrifier2d ago
that would need to be handled at the application level as better-auth will only manage the auth You can query the team which includes the user and display this to the user.
Sanjay Kholiya
will try that, thanks
Netrifier
Netrifier2d ago
if you are using nextjs then you can check the team in the page component which will run on the server and pass the team to the rest of the components.

Did you find this page helpful?