Skip to content
Back

`account.get()` sometimes times out in functions

  • 0
  • Self Hosted
  • Functions
  • Accounts
  • REST API
BasdP
25 Feb, 2024, 21:53

Hi,

I'm trying to provide an API to let users from a c++ app call some functions of my Appwrite site. I've written a function to get a Client that is authenticated with a JWT token, that also verifies that it can get a valid account using that token:

TypeScript
import { Client, Account } from 'node-appwrite';

export function getAnonClient() {
    return new Client()
        .setEndpoint('https://appwrite.cloud.virplugs.com/v1')
        .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID!);
}

export async function getUserClient(req: Request, verify = false) {
    let user_jwt = null;
    if (req.headers['x-appwrite-user-jwt']) {
        user_jwt = req.headers['x-appwrite-user-jwt'];
    } else if (req.headers['authorization']) {
        user_jwt = req.headers['authorization'].replace('Bearer ', '');
    } else {
        return null;
    }

    const userClient = getAnonClient().setJWT(user_jwt);

    if (verify) {
        try {
            const account = new Account(userClient);
            await account.get();
        } catch (e) {
            return null;
        }
    }

    return userClient;
}

The point of this function is to get a Client object that is authorized with a JWT token that the user provides (either through the x-appwrite-user-jwt header or a regular JWT Bearer token. I get the user JWT token by requesting /v1/account/sessions/email followed by a /v1/account/jwt. That works nicely.

The code above works 90% of the time. But sometimes the line await account.get(); just doesn't resolve. It hangs there until the fuction gets killed because of a timeout. If I retry the exact same call, it works. It's just very random when it's timing out.

Any pointers to what might causing this? Maybe I'm doing things very wrong. I'm using the node-20.0 runtime.

Also, I have no idea how to debug this, the docker logs show nothing interesting (even with development env var on)…

Kind regards, Bas

TL;DR
Issue with `account.get()` sometimes timing out in functions. Code provided to get a `Client` with authenticated JWT token. Occasional timeout on `await account.get()` call. Difficulty debugging. Solution: Consider implementing retry logic for the failing `await account.get();` call.
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