Back

[SOLVED] CORS error on client SDK localhost

  • 0
  • Web
Osman
5 Oct, 2023, 16:38

Hello, i get the following error when trying to access the createEmailSession endpoint in the client sdk: Access to XMLHttpRequest at 'http://192.168.x.x/v1/account/sessions/email' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. I have added both localhost and * as web platforms. The client is initialized like so:

TypeScript
client.setProject("64d...196").setEndpoint("http://192.168.x.x/v1")```
TL;DR
Summary: The user was experiencing a CORS error when trying to access the Appwrite server SDK on localhost. They initially used the IPv4 address as the domain, which worked until they upgraded to version 1.4.4. They also tried using localhost as the domain but encountered the same error. The user was advised that they can use localhost from the client app but not within an Appwrite function. They were also suggested to set the IP address as the domain environment variables in the Appwrite .env file. The user mentioned that they were verifying the user request JWT on the server and using account.get(), which resulted in a large error
Drake
5 Oct, 2023, 17:00

you should probably use https instead of just http. Also, make sure your project ID is correct. share more of the network logs if you're still having trouble

Osman
5 Oct, 2023, 17:04

Should i use https even though im working in dev environment on localhost?

Drake
5 Oct, 2023, 17:17

i typically do

Drake
5 Oct, 2023, 17:17

and i just accept the self signed cert

Osman
5 Oct, 2023, 17:45

Thanks for the tip

Seems its messed up on both the client SDK and server SDK.. I get this huge error on the server SDK when trying to call account.get() :

TypeScript
    at Client.call (/usr/local/server/src/function/node_modules/node-appwrite/lib/client.js:172:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Account.get (/usr/local/server/src/function/node_modules/node-appwrite/lib/services/account.js:29:16)
    at async APIContext.isUserValid (file:///usr/local/server/src/function/src/functions/api/code/APIContextClass.js:52:16)
    at async APIContext.setUserObject (file:///usr/local/server/src/function/src/functions/api/code/APIContextClass.js:33:29)
    at async Module.default (file:///usr/local/server/src/function/src/functions/api/code/main.js:19:9)
    at async execute (/usr/local/server/src/server.js:141:22)
    at async /usr/local/server/src/server.js:158:13 {
  code: 401,
  type: '',
  response: '<!DOCTYPE
Drake
5 Oct, 2023, 17:49

People typically don't use account.get() with a server SDK...what are you doing?

Osman
5 Oct, 2023, 17:58

Im basically verifying the user request jwt on the server, and with it, getting their role, which is used to have more fine-grained control of what can be accessed. Since the error appeared i havent changed any code, however, i did upgrade from 1.4.2 -> 1.4.3 -> 1.4.4

Drake
5 Oct, 2023, 18:01

Try to set the IP address as the domain environment variables in your Appwrite .env file

Osman
5 Oct, 2023, 18:07

Both APP_DOMAIN and APP_DOMAIN_TARGET?

Osman
5 Oct, 2023, 18:13

Well that worked.. Shouldn't it be working on localhost as well then?

Drake
5 Oct, 2023, 18:14

yes, it should

Osman
5 Oct, 2023, 18:18

I get connect ECONNREFUSED 127.0.0.1:80 when using localhost, trying to execute my function

Drake
5 Oct, 2023, 18:21

you're using localhost from within an Appwrite Function?

Osman
5 Oct, 2023, 18:24

When setting the client in the function like so: const client = new Client() .setProject('projectID') .setEndpoint("http://localhost/v1") .setKey(process.env.APPWRITE_APIFUNC_KEY)

Osman
5 Oct, 2023, 18:24

and then i get the above error when calloing account.get()

Drake
5 Oct, 2023, 18:24

inside an appwrite function, you cannot use localhost because the runtime container will try to connect to itself instead of appwrite

Drake
5 Oct, 2023, 18:25

on the host machine, you can browse to https://localhost to see the appwrite console

Osman
5 Oct, 2023, 18:30

Exactly, thats why i originally changed it to use the ipv4 adresse in the functions, which had previously worked fine until upgrading to 1.4.4. Now im getting the CORS error when trying to execute functions from the client side hosted on localhost:3000

Drake
5 Oct, 2023, 18:50

if you're appwrite instance is installed locally, you can use localhost from your client app. You just can't use it in an appwrite function

Osman
5 Oct, 2023, 19:15

Hm i cant seem to get it working with localhost as the domain and functions using the ipv4 address, no matter what i try i get the above errors. Only way i can make it work is by pointing everything to the ipv4 address. I guess thats what im gonna have to do

Drake
5 Oct, 2023, 19:20

in your appwrite function, you must use ip or domain because you cannot use localhost

Drake
5 Oct, 2023, 19:21

what is your problem using localhost from your web app?

Osman
6 Oct, 2023, 13:17

Since just using the ipv4 across the board worked fine, i just wrote some scripts that configure everything automatically with the given ipv4 of the local machine. Thank you for your help πŸ™‚

Drake
6 Oct, 2023, 14:13

[SOLVED] CORS error on client SDK localhost

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