Back

Trying to call an appwrite function does not return expected response

  • 0
  • Functions
  • Accounts
kstulgys
22 Jun, 2023, 20:21

I deployed appwrite function that returns user jwt:

TypeScript
module.exports = async function (req, res) {
  const jwt = req.variables.APPWRITE_FUNCTION_JWT;
  res.json({ jwt });
};

When triggered through admin api I can see jwt in the logs, but when triggered in my app (locally) serverside I see jwt as empty string:

TypeScript
      let token = await functions.createExecution('getCurrentUserJwt');

// token object:
 {
  token: {
    '$id': '6494abfd1dcc5d578e3b',
    '$createdAt': '2023-06-22T20:15:57.122+00:00',
    '$updatedAt': '2023-06-22T20:15:57.129+00:00',
    '$permissions': [],
    functionId: 'getCurrentUserJwt',
    trigger: 'http',
    status: 'completed',
    statusCode: 200,
    response: '{"jwt":""}',
    stdout: '',
    stderr: '',
    duration: 0.0022101402282714844
  }
}

Why is this? Appwrite is deployed to digital ocean.

TL;DR
The user is trying to call an "appwrite" function but is not receiving the expected response. They mention that the variable APPWRITE_FUNCTION_JWT will only have the JWT when the function is executed from a client-side or from a server-side using JWT instead of an API token. They discuss the idea of maintaining the user's JWT server-side without passing it from a cookie and initializing the server-side SDK using the JWT. They mention triggering a Next.js API endpoint and executing the function server-side. The user mentions that when the function is triggered through the admin API, they can see the JWT in the logs, but when triggered in their
Binyamin
22 Jun, 2023, 20:36

To br able to generate JWT the execution can be run only from a client side. As only when a client is executing the function, only then Appwrite will generate the JWT token

kstulgys
22 Jun, 2023, 20:44

Yes before calling appwrite function I generate jwt client side account.createJwt()

kstulgys
22 Jun, 2023, 20:45

Then call api endpoint that triggers createExecution

Binyamin
22 Jun, 2023, 20:45

Api endpoint?

Binyamin
22 Jun, 2023, 20:45

Waht you mean?

kstulgys
22 Jun, 2023, 20:46

I'm execution function serverside

kstulgys
22 Jun, 2023, 20:46

Triggering nextjs api endpoint

Binyamin
22 Jun, 2023, 20:46

Got it

Binyamin
22 Jun, 2023, 20:46

So in this use case you won't get the JWT automatically

Binyamin
22 Jun, 2023, 20:47

If you want to get you'll need to pass it to next.

Binyamin
22 Jun, 2023, 20:47

Then pass it to the function as data Or Initialize the server side SDK using the JWT

kstulgys
22 Jun, 2023, 20:48

My initial idea is to maintain users jwt serverside without passing it from cookie

kstulgys
22 Jun, 2023, 20:48

Does this make sense?

Binyamin
22 Jun, 2023, 20:49

Yes, Have you check almost ssr of next by Matej?

kstulgys
22 Jun, 2023, 20:49

?

kstulgys
22 Jun, 2023, 20:51

Oh, will take a look

kstulgys
22 Jun, 2023, 21:00

So getting back on my initial subject. The function will return jwt only if it's called client side, right?

Binyamin
22 Jun, 2023, 21:00

Yes

Binyamin
22 Jun, 2023, 21:01

What I mean is the variable APPWRITE_FUNCTION_JWT will have the JWT Only when:

  1. Executed from a client side
  2. Executed from a server side by using JWT instead of API token
kstulgys
23 Jun, 2023, 05:30

Git it

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