Back

Is it possible to get a Locale instance for a specific user in a cloud function?

  • 0
  • Users
  • Functions
  • Locale
  • Cloud
memoonlite
22 Feb, 2024, 09:22

Hi! 😊

I'd like to get the IP address of the user that initiates a cloud function. According to the documentation it should be possible to use the Locale API for that (this is from the Node.js, Cloud documentation):

TypeScript

// Init SDK
const client = new sdk.Client();

const locale = new sdk.Locale(client);

client
    .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
;

const promise = locale.get();

promise.then(function (response) {
    console.log(response);
}, function (error) {
    console.log(error);
});```

Executing the above code from a cloud function returns the IP address of the server (which in case of Appwrite Cloud is in Germany), which makes sense. This is however not the information I'm looking for. I'd like to retrieve the IP address of the user that initiates the function.

So I was thinking of getting the user first:

```var user = await users.get(req.headers["x-appwrite-user-id"]);```

But now I'm stuck. Does anyone know how I run locale.get(); on that user object, so that I can retrieve the IP address from that user? Or is what I'm trying to not possible?
TL;DR
Developers are trying to get the Locale instance for a specific user in a cloud function using the Node.js Cloud documentation. The issue is that when running the code provided, it returns the IP address of the server instead of the user initiating the function. They are looking to fetch the IP address of the user. Solution: To achieve this, developers can pass the user's IP address as a parameter to the cloud function or retrieve the user's IP address directly from the request headers using `req.headers['x-forwarded-for']` instead of trying to use Locale.
memoonlite
22 Feb, 2024, 09:23

Is it possible to get a Locale instance for a specific user in a cloud function?

memoonlite
22 Feb, 2024, 09:24

I'm always struggling to get the title right. πŸ˜‚

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