Back

I cant update the document even I have the permission in server side using JWT token

  • 1
  • Databases
  • Cloud
Mosh Ontong
24 Jun, 2024, 12:28
TypeScript
  const jwtClient = new Client();
    jwtClient
      .setEndpoint(Bun.env.APPWRITE_FUNCTION_ENDPOINT)
      .setProject(Bun.env.APPWRITE_FUNCTION_PROJECT_ID)
      .setJWT(token);
    const users = new Users(client);
    const functions = new Functions(client);
    const databases = new Databases(client);
    const jwtDatabases = new Databases(jwtClient);

/// .....

 const userDocument: UserDocument = await jwtDatabases.updateDocument(
      Bun.env.DATABASE_ID,
      Bun.env.USER_COLLECTION_ID,
      id,
      {
        name,
        phoneNumber,
        ...(email ? { email } : {}),
        ...(regionRef ? { regionRef } : {}),
        ...(provinceRef ? { provinceRef } : {}),
        ...(cityRef ? { cityRef } : {}),
        ...(barangayRef ? { barangayRef } : {}),

        enabled,
        role,
      }
    );
 log("User cache document updated");

Remember that I have log before and after of updateDocument

TL;DR
Developers are encountering a 401 error when attempting to update a document in their server-side application using a JWT token. Although the document is successfully updated in the database, the error suggests a lack of permission. The user, a superadmin, should have the necessary permissions. The log shows that the process is executed correctly. **Solution**: The error seems to be a discrepancy between the permissions associated with the document, particularly related to the superadmin role. Double-check the permissions and role designation for the user to ensure they have the required access levels for document updates.
Mosh Ontong
24 Jun, 2024, 12:28

This is the error response from execution:

TypeScript
 {"name":"AppwriteException","code":401,"type":"user_unauthorized","response":{"message":"Permissions must be one of: (any, users, user:65c86c61c9443bebd995, user:65c86c61c9443bebd995/verified, users/verified, label:superadmin)","code":401,"type":"user_unauthorized","version":"1.5.7"}}

And this is the logs:

TypeScript
Initializing Appwrite client
Setting API key for Appwrite client
Request received by /v1/users/666a6e20bd83c928b2e8 with method PUT
Triggered type: http
Event type: undefined
Matched route: /users/666a6e20bd83c928b2e8 with method PUT
userUpdateHandler triggered with 666a6e20bd83c928b2e8 and role techAssistant
Getting user details
Checking if the user is authorized to update a user
Updating user cache document
User cache document updated
Updating user details
Updating role
Role updated
Regenerating permissions
Permissions regenerated

As you can see it run through my codes of my function. Remember that I have log Updating user cache document -> call updateDocument -> User cache document updated. So it supposed to be not error right?. When I see my database it actually successfully updated the document but it give me an error that the user have not permission to update the document. The user actually have the permission to update that document because he is the superadmin

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