How to create a 8Base User with a role through a GraphQL mutation?

I’m working on an app using 8Base Users with Auth0. Login and registration is all possible and I can make any changes from the dashboard successfully.

One thing that I am trying to do from a React / GraphQL based app is write a mutation that will create a user with a pre-defined user role that I created in the 8Base dashboard. Right now, any user that signs up has “Guest” and “Administrator” by default, but I added an additional role called “Member”.

What would the mutation look like for a user that has the “Member” role? Here’s what I have so far with my User table having a Roles column (currently associated with Users table because I don’t know what else to try):

mutation SignUp(
  $email: String!
  $firstName: String!
  $lastName: String
  $password: String!
  $authProfileId: ID!
  $roleId: ID!
) {
userSignUpWithPassword(
  user: {
    email: $email
    firstName: $firstName
    lastName: $lastName
    roles: { connect: [{ id: $roleId }] }
  }
  password: $password
  authProfileId: $authProfileId
) {
  id
  email
  createdAt
  }
}

Hi there, when you use the userSignUpWithPassword mutation, you are setting the authProfileId. That Authentication Profile is where you’re able to set the roles that are added to a user once they sign up.

Otherwise, if it worked like this, and new user could technically assign themselves any roles they want.

So simply take roles: { connect: [{ id: $roleId }] } OUT of your mutation and add the Member role to your authentication profiles settings.

Hi, I am having same query, i understand what you have said but it is for one role ‘Member’ as far as i understand it.
What if there are 2 Roles: Teacher and Student and i want to assign roles while they sign up, i can not create 2 Auth Profile for each role right, How i can assign Teacher role to teacher signup and Student role to student sign up

One way to do that is with 2 auth profiles, though I wouldn’t really recommend it as there’s no need to segregate the two.

Try using an after trigger that updates the user role, depending on how you want to determine whether they are a Student or Teacher.

functions:
  afterUserCreate:
    type: trigger.after
    handler:
      code: src/triggers/afterUserCreate/handler.js
    operation: Users.create
module.exports = async (event, ctx) => {

   await ctx.api.gqlRequest(`
    mutation($id: ID!, $roles: [RoleKeyFilter!]) {
      userUpdate(data: {
        id: $id,
          roles: {
            connect: $roles
          }
        })
      }
   `, 
   {
    id: event.data.id,
    roles: determineRolesRequired(event.data)
  },
  { 
    checkPermissions: false 
  })

  return {
    data: {
      ...event.data
    }
  };
};
1 Like

Thank you so much. This is exactly what I want.

1 Like