Skip to content
Back

[SOLVED] (role: applications) missing scope (account)

  • 0
  • Functions
  • Cloud
Ankit Maniya
14 Apr, 2025, 21:21

Expectation: When I pass a user session ID to the cloud function via a REST API call, and I create a team, the team should be assigned to that user. If I request a list of teams, the cloud function should return the team list for that specific user.

TypeScript
// "aw" stands for Appwrite. Get the Appwrite API key and user session ID from HTTP request headers.
String awKey = context.req.headers['x-appwrite-key'] ?? '';
String awUserSessionId = context.req.headers['x-appwrite-user-session-id'] ?? '';

context.log("Appwrite Key: $awKey");
context.log("Appwrite User Session ID: $awUserSessionId");

// If either the key or session ID is present in the request, update the client configuration.
if (awKey.isNotEmpty || awUserSessionId.isNotEmpty) {
  client.setKey(awKey);
  client.setSession(awUserSessionId);
}

// "aw" stands for AppWrite
awDatabases = Databases(client);
awTeams = Teams(client);
awUsers = Users(client);
awAccount = Account(client);

// Get the user account based on the session ID.
User account = await awAccount!.get();

// Create a team and associate it with the authenticated user.
Team? team = await awTeams?.create(
  teamId: dbTeamId,
  name: pbMessage.friendlyName,
);
TL;DR
Developers had an issue with missing scope 'account' in their app, solved by using the session secret instead of session id for authentication. They were expecting to assign a team to a specific user when using a user session ID, done with 'setKey' and 'setSession' in a cloud function.
Ankit Maniya
14 Apr, 2025, 21:23

I am using dart_appwrite: ^13.0.0 in Cloud Function

Ankit Maniya
14 Apr, 2025, 21:24

API keys access

Kenny
14 Apr, 2025, 21:25

You wouldn't use a session and key on the same client

Kenny
14 Apr, 2025, 21:26

when using a session it makes the client act on behalf of the user that owns that session, but using an API key trumps this and makes it abide by the rules set on the key

Ankit Maniya
14 Apr, 2025, 21:27

I also tried without setKey but I got different error. Let me give you Screen Shot

Ankit Maniya
14 Apr, 2025, 21:28

It consider my request as guest

AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (account) (401) appwrite-user-session-id :: 67fd7db9606778198bc5

User account = await awAccount!.get();

print('account :: $account');

Kenny
14 Apr, 2025, 21:30

The session id can't be used like that, sorry I didn't realize. You would need the session secret. Otherwise if you have the user ID you could create the team then add that user to the team

Ankit Maniya
14 Apr, 2025, 21:31

OK let me tried with session secret. will it allow it ??

Kenny
14 Apr, 2025, 21:32

Wil it allow what?

Ankit Maniya
14 Apr, 2025, 21:33

Sorry for confusion.

In my cloud function, I am making login request Then it return me sessionSecret, sessionId

Then I am making call to [create the team/ list team] for current user in cloud function

Ankit Maniya
14 Apr, 2025, 21:34

So if I set session secret then it will allow me to create team for that user

Kenny
14 Apr, 2025, 21:34

yes it should

Ankit Maniya
14 Apr, 2025, 21:35

Let me give a try

Ankit Maniya
14 Apr, 2025, 21:36

One more question,I have to use this client.setSession and I have to pass sessionSecret instead of sessionId right?

Kenny
14 Apr, 2025, 21:37

yes

Ankit Maniya
14 Apr, 2025, 21:54

Thank you so much @Kenny, It worked as expected.

But I am wonder why in documentation, it mention like user session

Instead it might be in more details user session secret

or I might missed something.

TypeScript
import 'package:dart_appwrite/dart_appwrite.dart';
Client client = Client()
    .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
    .setProject('<YOUR_PROJECT_ID>') // Your project ID
    .setSession(''); // The user session to authenticate with
Ankit Maniya
14 Apr, 2025, 21:55

[SOLVED] (role: applications) missing scope (account)

Ankit Maniya
14 Apr, 2025, 21:56
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