Concepts

Teams

Manage teams and team members

Teams provide a structured way to group users and manage their permissions. Users can belong to multiple teams, which can represent departments, B2B customers, or projects.

To assign users to a default team upon sign-up, activate the corresponding toggle in the Stack dashboard under the team settings tab. This setting automatically assigns each new user to a team.

Creating a Team

To create a team, use the createTeam method on the stackServerApp. Here’s an example:

Create a Team
1const team = await stackServerApp.createTeam({
2 displayName: 'New Team',
3});

Adding a User to a Team

To add a user to a team, use the addUser method on the Team object. Here’s how:

Add a User to a Team
1const team = await stackServerApp.getTeam('teamId');
2await team.addUser(user);

List All the Teams of a User

You can list all the teams a user belongs to by using the listTeams method or the useTeams hook on the User object. Here’s how to do it:

List teams of a user on the client
1"use client";
2import { useUser } from "@stackframe/stack";
3
4export function DisplayUserTeams() {
5 const user = useUser({ or: 'redirect' });
6 const teams = user.useTeams();
7
8 return (
9 <div>
10 {teams.map(team => (
11 <div key={team.id}>{team.displayName}</div>
12 ))}
13 </div>
14 );
15}

Get Specific Team of a User

To obtain details of a specific team a user belongs to, use the getTeam method or useTeam hook. Note: this might return null if the user is not a member of that team. Here’s an example:

Get a specific team of a user on the client
1"use client";
2import { useUser } from "@stackframe/stack";
3
4export function DisplayUserTeam(props: { teamId: string }) {
5 const user = useUser({ or: 'redirect' });
6 const team = user.useTeam(props.teamId);
7
8 return (
9 <div>
10 {team ? team.displayName : 'Not a member of this team'}
11 </div>
12 );
13}

List All the Teams

To list all teams, use the listTeams method on the stackServerApp. Here’s an example:

1const teams = await stackServerApp.listTeams();

Update a Team

To update a team, use the update method on a server-side team object. Here’s how to do it:

1const team = await stackServerApp.getTeam('teamId');
2await team.update({
3 displayName: 'New Team Name',
4});

Remove a User from a Team

To remove a user from a team, use the removeUser method on the team object. Here’s an example:

1const team = await stackServerApp.getTeam(teamId);
2await team?.removeUser(userId);

List All the Users of a Team

To list all the users of a team, use the listUsers method on a server-side team object. Here’s an example:

1const team = await stackServerApp.getTeam('teamId');
2const users = await team.listUsers();
3for (const user of users) {
4 console.log(`${user.displayName} is a member of ${team.displayName}`);
5}