
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.
// "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,
);

I am using dart_appwrite: ^13.0.0
in Cloud Function

API keys access

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

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

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

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');

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

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

Wil it allow what?

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

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

yes it should

Let me give a try

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

yes

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.
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

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

Any idea about this issue https://discord.com/channels/564160730845151244/1359934576062890145
Recommended threads
- Bypass Error When Creating Account With ...
Suppose user first uses email/pass for log in using xyz@gmail.com, few month later on decides to use google oauth2 with same xyz@gmail.com (or in reverse orde...
- dart function very slow
sometimes waiting too long, about 3mins to 5mins, sometimes very fast, not build time, just execute, anyway to speed up?
- wrong code for google oauth2 ?
gives User (role: guests) missing scope (account) error
