Back

Function Permission Error

  • 0
  • React Native
  • Auth
  • Functions
  • Cloud
IamtheFuture
29 Sep, 2024, 15:24

LOG Error creating driver [AppwriteException: Missing "execute" permission for role "users". Only "["any","guests"]" scopes are allowed and "["users"]" was given.] LOG Error creating driver [Error: AppwriteException: Missing "execute" permission for role "users". Only "["any","guests"]" scopes are allowed and "["users"]" was given.]

TypeScript
try {
    const newuser = await account.create(
      ID.unique(),
      user.email,
      user.password,
      name
    );

    const res = await functions.createExecution(
      CREATE_LABEL_FUNCTION_ID,
      JSON.stringify(`{userId: ${newuser.$id}, label: "driver"}`),
      true,
      "/",
      ExecutionMethod.POST
    );

    return { user: newuser };
  } catch (error: any) {
    console.log("Error creating driver", error);
    throw new Error(error);
  }
TL;DR
A developer is encountering a permission error when trying to update user labels and names based on user type. The solution involves setting the name during user creation, updating the label accordingly, and utilizing events in function settings. Additionally, an error occurred due to missing execution permission for the "users" role, which was resolved by adjusting the function settings and using the respective permissions.
faye
29 Sep, 2024, 15:55

Hey.

When creating an account, you are not logged in. You need to login first and then use the function

faye
29 Sep, 2024, 15:56

Or, a better way, is to use events in the function settings.

IamtheFuture
29 Sep, 2024, 15:59

oh great, please how do use event to detect the type of user that register, and set the label for that user. My app two type of users (users,driver)

faye
29 Sep, 2024, 16:00

You could use something like users.*.create. Check the functions settings tab.

faye
29 Sep, 2024, 16:01

In your function you can then use const document = req.body;, where document will contain the data of your user.

Just use log(document) and you can see what it contains. It should contain the user document.

IamtheFuture
29 Sep, 2024, 16:05

ok, this is good, but the user create function

TypeScript
const newuser = await account.create(
      ID.unique(),
      user.email,
      user.password,
      name
    );

does not have any field where I could add a custom tag(or something), so that I can check for it const document = req.body; and then add my desired label to the user

faye
29 Sep, 2024, 16:06

In your function you need to update the user to add the label, you can get the user id from document.userId.

IamtheFuture
29 Sep, 2024, 16:12

ok, i think i get how to go about it now

on user creation i will set the name to user type (driver or user) then update the label based on the name

then whenever the user logs in on my app, i will check there name and if it is still driver or user, i will update it with there real name that i already save on a profile document while creating their account

IamtheFuture
29 Sep, 2024, 16:23

@faye this is my final solution, though am yet to test it

TypeScript
export default async ({ req, res, log, error }) => {
  try {
    const client = new Client()
      .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT)
      .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
      .setKey(req.headers['x-appwrite-key'] ?? '');

    // console.log(req.headers['x-appwrite-key']);

    const users = new Users(client);

    const { name, userId } = JSON.parse(req.body);
    const label = name.split('-')[1];
    const finalName = name.split('-')[0];

    log('req', typeof req.body);
    log('userId', userId);

    await users.updateLabels(userId, [label]);
    await users.updateName(userId, finalName);

    return 'User label updated successfully';
  } catch (err) {
    log('Could not list users: ' + err.message);
    error('Could not list users: ' + err.message);
  }
};
TypeScript
const name = `${user.firstName} ${user.lastName}-driver`;

  try {
    const newuser = await account.create(
      ID.unique(),
      user.email,
      user.password,
      name
    );

    // create driver document
    await createDriverDoc(user, newuser.$id);

    return { user: newuser };
  } catch (error: any) {
    console.log("Error creating driver", error);
    throw new Error(error);
  }
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more