Back

The current user is not authorized to perform the requested action / 'Permissions must be one of....

  • 0
  • Databases
  • Web
_alnes_
20 Apr, 2023, 11:16

Angular / REST for Database / SDK for Auth Appwrite 1.3.1

Hello πŸ™‚

I've been trying to do this for a few hours, but unfortunately I can't get it to work. I used to have a project entirely through the Appwrite SDK, but now I want to do all database actions through REST.

The get Methods works. But the post/patch dont.

Here is one example:

TypeScript
  updateIngredient(ingredient: Ingredient) {

    const collectionId = '630f4c60cfef19f89bf6';

    return this.http.patch(
      `${this.baseUrl}/databases/${this.databaseId}/collections/${collectionId}/documents/${ingredient.$id}`,
      {
        'data': {
          'name': ingredient.name,
          'namePlural': ingredient.namePlural
        },
        'permissions': ['write("any")']
      },
      {headers: this.headers}
    ).pipe(
      finalize(() => console.log('updateIngredient ' + ingredient.name + ' success'))
    )
  }
```   So, with any it works.
TL;DR
The user is getting a 'Permissions must be one of: (any, guests)' error when trying to perform a database update using the Appwrite SDK and REST together. The user has an active session and the necessary permissions, but the request is still unauthorized. When trying to use specific user permissions, a different error is thrown. The user is unsure if using the SDK and REST together in one project is a good idea. A possible solution is to try using the SDK instead of REST for the database actions.
_alnes_
20 Apr, 2023, 11:26

But if i want to use a user: ```ts updateIngredient(ingredient: Ingredient) {

TypeScript
const collectionId = '630f4c60cfef19f89bf6';

return this.http.patch(
  `${this.baseUrl}/databases/${this.databaseId}/collections/${collectionId}/documents/${ingredient.$id}`,
  {
    'data': {
      'name': ingredient.name,
      'namePlural': ingredient.namePlural
    },
    'permissions': ['write("user:630f4a50d9371ce4576d")']
  },
  {headers: this.headers}
).pipe(
  finalize(() => console.log('updateIngredient ' + ingredient.name + ' success'))
)

}``` I get this error:

_alnes_
20 Apr, 2023, 11:27

data-dialog.component.ts:57 ERROR HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: 'OK', url: 'https://appwrite.krach.ist-im-netz.at/v1/databases…0f4c60cfef19f89bf6/documents/6320b5d918defba35028', ok: false, …} error : {message: 'Permissions must be one of: (any, guests)', code: 401, type: 'user_unauthorized', version: '1.3.1'} headers : HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: Ζ’} message : "Http failure response for https://appwrite.krach.ist-im-netz.at/v1/databases/630f4596b5440bd5a44a/collections/630f4c60cfef19f89bf6/documents/6320b5d918defba35028: 401 OK" name : "HttpErrorResponse" ok : false status : 401 statusText : "OK" url : "https://appwrite.krach.ist-im-netz.at/v1/databases/630f4596b5440bd5a44a/collections/630f4c60cfef19f89bf6/documents/6320b5d918defba35028" [[Prototype]] : HttpResponseBase

_alnes_
20 Apr, 2023, 11:27

As can be seen in the picture above, the user has the permissions.

_alnes_
20 Apr, 2023, 11:28

And the user has an active session

_alnes_
20 Apr, 2023, 11:40

Hm, it works with any and guests. So, there must be a user_unauthorized issue

_alnes_
20 Apr, 2023, 11:41

but i can login/logout and the angular guards works also good with appwrite session-checks

_alnes_
20 Apr, 2023, 12:01

Hm. I have to send the cookie? I get that from the Response.Headers. But since I want to do the Auth stuff via Sdk, I can't get there?! Maybe not a good idea to use SDK and REST together in one project?

_alnes_
20 Apr, 2023, 12:58

Okay, i will create a JwtToken and send it as X-Appwrite-JWT in the REST call. Maybe...

_alnes_
20 Apr, 2023, 13:35

This was the issue

_alnes_
20 Apr, 2023, 13:36
TypeScript

  private headers = new HttpHeaders()
    .set('Content-Type', 'application/json')
    .set('X-Appwrite-Response-Format', '1.0.0')
    .set('X-Appwrite-Project', 'ngchat')

  updateIngredient(ingredient: Ingredient) {

    const collectionId = '630f4c60cfef19f89bf6';

    if (this.jwtToken) this.headers = this.headers.set('X-Appwrite-JWT', this.jwtToken);
    

    return this.http.patch(
      `${this.baseUrl}/databases/${this.databaseId}/collections/${collectionId}/documents/${ingredient.$id}`,
      {
        'data': {
          'name': ingredient.name,
          'namePlural': ingredient.namePlural
        },
        'permissions': ['write(\"users\")']
      },
      {
        headers: this.headers
      }
    ).pipe(
      finalize(() => console.log('updateIngredient ' + ingredient.name + ' success'))
    )
  }
Drake
20 Apr, 2023, 18:04

Why don't you want to use the SDK?

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