Back

account.get() in server function does not get user from react

  • 0
  • Self Hosted
  • Functions
  • Web
KingBob
16 Nov, 2023, 23:03

frontend:React backend: nodejs

Main Issue: I have the user logged in in React and call a server function. On the serverside nodejs function I call await account.get() but it isn't detecting the logged in user.

I call the function in react using: await functions.createExecution(functionId) Permissions: I have allowed users to use the function in the Execute Access settings of the function and selected "Users"

Result of the server function after calling it :" User (role: guests) missing scope (account)

I am new to Appwrite so maybe I am missing something. Thanks in advance

TL;DR
The user is not being detected when calling `account.get()` in the server function. The user ID can be retrieved from the JWT token passed in the headers. Set the JWT token using `setJWT()` instead of `setKey()`. Also, make sure that the user has the necessary scope (account) and the function has the correct permissions. Double-check that the `APPWRITE_ENDPOINT` is set correctly.
Drake
17 Nov, 2023, 03:25

Did you do anything else before account.get()? How are you setting the user?

KingBob
18 Nov, 2023, 23:00

@Steven I use import { Account, Client } from 'node-appwrite'; and initialize the client and account like this: const client = new Client(); client .setEndpoint(process.env.APPWRITE_ENDPOINT) .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) .setKey(process.env.APPWRITE_API_KEY);

const account = new Account(client); and then I do: try { // Get user details to check authentication const user = await account.get();

TypeScript
// If user details are received, the user is authenticated
return res.json(JSON.stringify({ authenticated: true, user }));

} catch (err) { // If there's an error or user details are not received, the user is not authenticated return res.json(JSON.stringify({ authenticated: false, error: err.message })); }

KingBob
18 Nov, 2023, 23:09

The env variable APPWRITE_ENDPOINT is my private ip address 192.168.1.x. I also set my APPWRITE_API_KEY and PROJECT_ID in the console. My react app APPWRITE_ENDPOINT is also set to the same private ip address. I tried using http://localhost/v1 in my server function but I kept getting a connection error, and had to change it to my private ip for it to work anyway I am still getting the same error: "User (role: guests) missing scope (account)"

KingBob
18 Nov, 2023, 23:26

I am now getting (role: applications) missing scope (account)

Drake
18 Nov, 2023, 23:51

You can't use account.get() with an API key

Drake
18 Nov, 2023, 23:52

So when a user executes a function, a JWT token is created and passed to the function in the headers. You'll need to get that and call setJWT() instead of setKey()

KingBob
18 Nov, 2023, 23:56

Thank you @Steven I will try that.

omega-zorg
19 Nov, 2023, 00:07

@Steven how would I retrieve the user ID after setting the JWT token?

Drake
19 Nov, 2023, 00:07

If you only need the user id, there's a header with that already.

omega-zorg
19 Nov, 2023, 00:08

yeah, but if I want to get the user ID from the JWT token to make sure it is the right user

omega-zorg
19 Nov, 2023, 00:08

btw, thanks for your help 🙂

Drake
19 Nov, 2023, 00:09

It will be the right user.

But how does a user get their user id in general (let's say client side)?

omega-zorg
19 Nov, 2023, 00:11

I dont know 🙂 I am just starting, but I will have to make some queries that belong on the user relation

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