#CORS Errors with Extension

61 messages · Page 1 of 1 (latest)

regal raft
#

Hey there,

I am building an internal extension for my own directus, not to be used publicly. I originally performed a lot of requests to/from a URL that is another business I work with wthin Directus Flows and had no issues. I started developing a Module Extension that was able to do much more that what I was doing already. I wanted to be able to send data to this same URL but now I get CORS issues stating:

from origin 'http://localhost:8055' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

It is localhost because I am still testing the extension from a local directus instance. I am just performing a standard fetch GET request in the module just to test the connection. Does anyone know how to fix this issue without having to request assistance/access from the requested URL as they will take forever to respond.

dusky rainBOT
#

Thanks for posting! This is a community powered server, so you may or may not get an answer based on available help and expertise. To increase your chances of somebody being able to help you, please help us help you making sure you:

  • Adding an explanation of exactly what you're trying to achieve.
  • Adding any and all related code or previous attempts.
  • Describing the exact issue or error you are facing.
  • Posting any screenshots if applicable.
  • Reading through https://stackoverflow.com/help/how-to-ask.

When you're done with this thread, please close it. Thanks! ✨

(If you have a support agreement and need help, please contact the core team via email.)

next tulip
#

you need to configure CORS properly when cross-domain requests are in place

valid yoke
#

Like wolfulus says.
or, back in the day this also occured when the Directus instance crashed and if it was running after a load balancer, not sure if thats still the case tho

regal raft
#

I have CORS_ENABLED=true and CORS_ORIGIN=true

#

this is in my .env:

Whether or not to enable the CORS headers [false]

CORS_ENABLED=false

Value for the Access-Control-Allow-Origin header. Use true to match the Origin header, or provide a domain or a CSV of domains for specific access [false]

CORS_ORIGIN=false

Value for the Access-Control-Allow-Methods header [GET,POST,PATCH,DELETE]

CORS_METHODS=GET,POST,PATCH,DELETE

Value for the Access-Control-Allow-Headers header [Content-Type,Authorization]

CORS_ALLOWED_HEADERS=Content-Type,Authorization

Value for the Access-Control-Expose-Headers header [Content-Range]

CORS_EXPOSED_HEADERS=Content-Range

Whether or not to send the Access-Control-Allow-Credentials header [true]

CORS_CREDENTIALS=true

Value for the Access-Control-Max-Age header [18000]

CORS_MAX_AGE=18000

#

didn't mean for the mde

next tulip
#

you want to make a request to another server right?

#

in this case CORS needs to be configured in the target server

regal raft
#

yeah

next tulip
#

not yours

stuck forge
#

It is hard to debug without knowing more about the requests you're doing. When something errors it is possible that the CORS headers may not have been set yet

regal raft
#

How come it works when I perform the requests from Flows?

next tulip
#

what you can do if you don't have access to the target server (to configure CORS headers) is to create your own endpoint to act like a proxy, so you're still dealing with your own server

regal raft
#

yeah I did see about doing that tbh

#

just seemed strange that it would work fine with directus flows but not from an extension

next tulip
#

because flows runs on the server

#

cors is a browser only thing

#

in this case flows were acting like your proxy

#

if you only have 1 "path"

#

like server.com/path

regal raft
#

Oh I see, and there's no way I can utilise that somehow?

next tulip
#

a flow with webhook trigger can do that for you

regal raft
#

would that involve setting up some custom trigger or something?

next tulip
#

not a custom trigger, just a webhook trigger which is builtin

regal raft
#

ah, so similar to using useApi() in some way? I know it would probably be simpler for me to set up a proxy ofc as I am still only learning JS and Vue.js. I am running the website on a DigitalOcean server so there's probably some way I can utilise that

next tulip
#

unsure about useApi

#

you can make a proxy using flows btw

#

the final url will be ugly though, and there's a lot to cover

stuck forge
#

You should be able to use useApi from the browser to call a Flow trigger

next tulip
regal raft
#

yeah I feel that either contacting the other company to allow access or setting up a proxy separately may be the best route. Essentially the GET requests aren't too much of an issue for the URL I don't have to do those. My issue is that the way I have to make requests to this URL in order to add data to their database is lengthy and not straightforward and involves a lot of different requests. Furthermore, access to the server involves requesting a cookie and then using that cookie with every request and refreshing said cookie when it runs out. It's a painful process that I thought would just be easier hand-coding rather than building in flows

next tulip
regal raft
#

But yeah, I could use Flows to at least handle the data handover right?

next tulip
regal raft
#

as per your images

next tulip
#

it really depends on what you want to do, it's not going to be a clean proxy

#

btw @stuck forge would be nice if the webhook url could forward anything that comes after the id part to the flow itself, making the path available to the flow
/flows/trigger/[id]/[path*]

regal raft
#

at least this way I could hide login credentials in the .env as well. Something I wasn't able to do easily with custom extensions

stuck forge
next tulip
next tulip
regal raft
#

haven't seen any of those but good to know!

stuck forge
next tulip
#

I'm not sure what you mean by post trigger though, literally a flow trigger or setting POST in webhook trigger?

next tulip
#

on what I meant was the flow could still trigger even if it was requested with more path parameters

POST /flows/trigger/<id> -> $trigger.payload.path = "/"
POST /flows/trigger/<id>/some/other/path -> $trigger.payload.path = "/some/other/path"

stuck forge
next tulip
#

but anyways, went sideways on the topic, nevermind

stuck forge
#

ah yeah sorry for derailing the conv 🙈

next tulip
#

my bad

regal raft
#

yeah to quickly reaffirm, your idea of utilising directus flows as a proxy was a great idea and I'm utilising it now with success!

next tulip
#

😬

regal raft
#

so many thank you to you guys!

#

<3

next tulip
#

good luck

#

that was fast

#

;D

regal raft
#

fortunately I had mostly everything set up, the proxy, it turns out, was the final hurdle