Managing roles and users
With the UserRoles recipe you can:
- Assign roles to users
- Remove roles from users
- Get a list of all roles assigned to a specific user
- Get a list of all users that are assigned a specific role
#
Assign roles to a userYou can assign roles to users before hand or immediately after they sign up. The role must be created before you can assign it.
- NodeJS
- GoLang
- Python
import UserRoles from "supertokens-node/recipe/userroles";
async function addRoleToUser(userId: string) { const response = await UserRoles.addRoleToUser(userId, "user");
if (response.status === "UNKNOWN_ROLE_ERROR") { // No such role exists return; }
if (response.didUserAlreadyHaveRole === true) { // The user already had the role }}
import ( "github.com/supertokens/supertokens-golang/recipe/userroles")
func addRoleToUser(userId string) { response, err := userroles.AddRoleToUser(userId, "user", nil) if err != nil { // TODO: Handle error return }
if response.UnknownRoleError != nil { // No such role exists return }
if response.OK.DidUserAlreadyHaveRole { // The user already had the role }}
- Asyncio
- Syncio
from supertokens_python.recipe.userroles.asyncio import add_role_to_userfrom supertokens_python.recipe.userroles.interfaces import UnknownRoleError
async def add_role_to_user_func(user_id: str, role: str): role = "user" res = await add_role_to_user(user_id, role) if isinstance(res, UnknownRoleError): # No such role exists return
if res.did_user_already_have_role: # User already had this role pass
from supertokens_python.recipe.userroles.syncio import add_role_to_userfrom supertokens_python.recipe.userroles.interfaces import UnknownRoleError
def add_role_to_user_func(user_id: str, role: str): role = "user" res = add_role_to_user(user_id, role) if isinstance(res, UnknownRoleError): # No such role exists return
if res.did_user_already_have_role: # User already had this role pass
#
Remove role from a userYou can remove roles from a user, the role you provide will be removed only if the user was assigned that role.
- NodeJS
- GoLang
- Python
import UserRoles from "supertokens-node/recipe/userroles";
async function removeRoleFromUser(userId: string) { const response = await UserRoles.removeUserRole(userId, "user");
if (response.status === "UNKNOWN_ROLE_ERROR") { // No such role exists return; }
if (response.didUserHaveRole === false) { // The user was never assigned the role }}
import ( "github.com/supertokens/supertokens-golang/recipe/userroles")
func removeRoleFromUser(userId string) { // const response = await UserRoles.removeUserRole(userId, "user"); response, err := userroles.RemoveUserRole(userId, "user", nil) if err != nil { // TODO: Handle error return }
if response.UnknownRoleError != nil { // No such role exists return }
if response.OK.DidUserHaveRole == false { // The user was never assigned the role }}
- Asyncio
- Syncio
from supertokens_python.recipe.userroles.asyncio import remove_user_rolefrom supertokens_python.recipe.userroles.interfaces import UnknownRoleError
async def remove_user_role_func(user_id: str, role: str): res = await remove_user_role(user_id, role) if isinstance(res, UnknownRoleError): # No such role exists return
if res.did_user_have_role == False: # The user was never assigned the role pass
from supertokens_python.recipe.userroles.syncio import remove_user_rolefrom supertokens_python.recipe.userroles.interfaces import UnknownRoleError
def remove_user_role_func(user_id: str, role: str): res = remove_user_role(user_id, role) if isinstance(res, UnknownRoleError): # No such role exists return
if res.did_user_have_role: # User actually had this role pass
#
Get all roles for a userYou can get a list of all roles that were assigned to a specific user.
- NodeJS
- GoLang
- Python
import UserRoles from "supertokens-node/recipe/userroles";
async function getRolesForUser(userId: string) { const response = await UserRoles.getRolesForUser(userId); const roles: string[] = response.roles;}
import ( "github.com/supertokens/supertokens-golang/recipe/userroles")
func getRolesForUser(userId string) { response, err := userroles.GetRolesForUser(userId, nil) if err != nil { // TODO: Handle error return } _ = response.OK.Roles}
- Asyncio
- Syncio
from supertokens_python.recipe.userroles.asyncio import get_roles_for_user
async def get_roles_for_user_func(user_id: str): _ = (await get_roles_for_user(user_id)).roles
from supertokens_python.recipe.userroles.syncio import get_roles_for_user
def get_roles_for_user_func(user_id: str): _ = get_roles_for_user(user_id).roles
#
Get all users that have a roleYou can get a list of all users that were assigned a specific role, the getRolesForUser
returns a list of user ids.
- NodeJS
- GoLang
- Python
import UserRoles from "supertokens-node/recipe/userroles";
async function getUsersThatHaveRole(role: string) { const response = await UserRoles.getUsersThatHaveRole(role);
if (response.status === "UNKNOWN_ROLE_ERROR") { // No such role exists return; }
const users: string[] = response.users;}
import ( "github.com/supertokens/supertokens-golang/recipe/userroles")
func getUsersThatHaveRole(role string) { response, err := userroles.GetUsersThatHaveRole(role, nil) if err != nil { // TODO: Handle error return }
if response.UnknownRoleError != nil { // No such role exists return }
_ = response.OK.Users}
- Asyncio
- Syncio
from supertokens_python.recipe.userroles.asyncio import get_users_that_have_rolefrom supertokens_python.recipe.userroles.interfaces import UnknownRoleError
async def get_users_that_have_role_func(role: str): res = await get_users_that_have_role(role) if isinstance(res, UnknownRoleError): # No such role exists return
_ = res.users
from supertokens_python.recipe.userroles.syncio import get_users_that_have_rolefrom supertokens_python.recipe.userroles.interfaces import UnknownRoleError
def get_users_that_have_role_func(role: str): res = get_users_that_have_role(role) if isinstance(res, UnknownRoleError): # No such role exists return
_ = res.users