Back

Get team fail in appwrite function

  • 1
  • Functions
  • Web
  • Cloud
Horsty - Cyril - MagicTools
13 Dec, 2024, 18:02

I try to get team of a user inside appwrite function, but i get this error:

AppwriteException: User (role: guests) missing scope (teams.read)

If i try on client side, it's working fine. I don't understand why.

There is my cloud function sample code

TypeScript
    import { Client, Teams } from "node-appwrite";

//... inside function
    const userJwt = req.headers["x-appwrite-user-jwt"];
    const client = new Client()
      .setEndpoint("https://cloud.appwrite.io/v1")
      .setProject(process.env.APPWRITE_PROJECT_NAME)
      .setSession(userJwt); 
    log('X')
    const teams = new Teams(client);
    log('Y')
    const listTeam = await teams.list(); // <-- It's failed
    log('Z') // <-- Didn't get this log
TL;DR
Developers are experiencing issues getting the team of a user within an appwrite function, receiving an error message indicating authentication problems due to missing scopes. The solution involves using `setJWT()` instead of `setSession(userJwt)`. This approach resolves the error and allows the function to retrieve the user's team successfully.
Horsty - Cyril - MagicTools
13 Dec, 2024, 18:10

It's working on client side

TypeScript
import { Client, Teams } from "appwrite";

const client = new Client();

client
  .setEndpoint("endpoint") // Your Appwrite Endpoint
  .setProject("projectid"); // Your project ID
export const teams = new Teams(client);
const teamslist = await teams.list();

console.log(teamslist); // <-- get the team of the user
Horsty - Cyril - MagicTools
13 Dec, 2024, 18:10

On appwrite function i try to get the team of the same user

Horsty - Cyril - MagicTools
13 Dec, 2024, 18:38

@D5 there are here i have shared code that lead to auto block =/

Steven
13 Dec, 2024, 18:52

role: guests this means you're unauthenticated.

setSession(userJwt) you shouldn't be passing the JWT to setSession(). it should be setJWT()

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