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
- router_deployment_not_found
I updated my function a few times and now i am getting the error: router_deployment_not_found I even reverted back to my original code but i am still getting th...
- Cloud function deploy stucks in processi...
Been trying for the last hours to deploy my function but for whatever reason, alwasy stuck on processing!
- One-time Cloud migration blocked by data...
Hi, Iβm blocked on a one-time migration from Appwrite Cloud to my self-hosted Appwrite instance. We already fixed the region issue, and the migration now corre...