-
Notifications
You must be signed in to change notification settings - Fork 298
Cors blog #541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Cors blog #541
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3927d1e
Labels and teams blog
LauraDuRy b6540a5
Updated code block
LauraDuRy 8b863fd
labels blog cover
LauraDuRy faf83bf
images added for blog
LauraDuRy 13cec30
added images
LauraDuRy 65998fb
labels image
LauraDuRy 5d4f98a
added images
LauraDuRy 69273aa
Image folder restructured. Article cover added
divanov11 6dca5c5
Updated section headlines
divanov11 e212c3b
Cors article added
divanov11 216ff9a
Merge branch 'main' into cors-blog
divanov11 4997404
updated CORS blog
LauraDuRy 245852e
Update src/routes/blog/post/cors-error/+page.markdoc
LauraDuRy c0c8ef5
Update src/routes/blog/post/cors-error/+page.markdoc
LauraDuRy ddcf533
Update +page.markdoc
LauraDuRy 0e37aa3
Fix grammar + formatting in CORS blog
b67dab2
Updated typo in image
divanov11 643556a
Update cover image
8609acc
Fix image file extension
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| --- | ||
| layout: post | ||
| title: Solving CORS errors with Appwrite | ||
| description: Understanding why you are getting CORS error when sending a request to an Appwrite backend and how to debug. | ||
| date: 2024-01-16 | ||
| cover: /images/blog/cors-error/cors_cover.png | ||
| timeToRead: 3 | ||
| author: dennis-ivy | ||
| category: engineering | ||
| featured: false | ||
| --- | ||
|
|
||
| I want to address an issue I've seen popping up on Stack Overflow and the Appwrite Discord server and address some of the reasons you may be getting this error, then walk you through some of the steps you can take to try and resolve it as well. | ||
|
|
||
| The error message you'll see in your console when trying to make a request to an Appwrite backend will look something like this: `Access blocked by CORS policy.` | ||
|
|
||
| # Understanding CORS | ||
|
|
||
| Before we start debugging this, let's talk about what a CORS error is. | ||
|
|
||
| Without diving deep into the topic, CORS (Cross-Origin Resource Sharing ) is a mechanism that allows a server to specify which origins can access it. By origins, I mean URLs. | ||
|
|
||
| Site A, our server sitting at `myapi.com` won't allow request coming from site B, which is our client app sitting at `myfrontend.com`. | ||
|
|
||
| This happens because our server has not added site B, `myfrontend.com`, to its list of allowed origins. | ||
| Any request coming from a URL that is not listed in our server's allowed origins will be rejected by our CORS policy. | ||
| The solution in this case would be to simply add `myfrontend.com` to the list of allowed origins. | ||
|
|
||
|  | ||
|
|
||
| CORS is crucial because it provides a secure way to make requests across different origins. | ||
| Without CORS, any website would be able to make requests to our server, and this would lead to major problems. | ||
| Imagine a malicious third party making a website `myfr0nt3nd.com` that key logs your user name and password, before making requests to your backend to validate the combination. | ||
|
|
||
| # Why you are getting a CORS error | ||
|
|
||
| Now let's try to figure this all out in the context of Appwrite and why you may be getting this error. | ||
| I have listed three main reasons. If more arise, I will update the article to include them. | ||
|
|
||
| 1. Origin not set in Console | ||
| 2. Origin is set incorrectly | ||
| 3. Bad ID on request | ||
|
|
||
| ## 1 - Origin not set in Console | ||
|
|
||
| First, you'll want to check your Appwrite Console to make sure you have added a hostname and are making a request from the correct hostname. | ||
| Make sure you have added a platform in your Appwrite console by going to the **Overview** tab, select your platform or adding one if you have none, and then ensure you have added a hostname. | ||
|
|
||
| A hostname is simply the domain you will be making the request from. In development, this will most likely be `localhost`. No need to add a port number or protocol here. | ||
|
|
||
| ## 2 - Origin is set incorrectly | ||
|
|
||
| The second issue I often see is a hostname that is not properly configured. This is usually a typo or, more often, a mistake made when switching between a local domain and live domain. Developers will often deploy their website and wonder why they are getting this error, only to find they still have their hostname set as localhost. | ||
|
|
||
| So if you find this is why you were getting a CORS error, you have a few ways of solving this. | ||
|
|
||
| 1. Update hostname each time you switch between localhost and live URL. This is not ideal since you would be switching back and forth constantly. | ||
| 2. Add a wildcard to allow requests from any origin - Not secure | ||
| 3. Add multiple origins - This can be done by adding another "platform" and simply specifying the second and third origins as you add them. - Recommended | ||
|
|
||
| ## 3 - Incorrect ID on request | ||
|
|
||
| This one happens because of an improperly configured request, such as a typo when specifying a project ID. For example, when using the `listDocuments` method, | ||
| if the project ID is set incorrectly when the client is initialized, you will receive a CORS error. | ||
|
|
||
| Without diving into the details about how CORS works, the problem occurs when the browser tries to check if the origin is allowed. | ||
| The request returns a `40X` response, so the entire CORS check fails. | ||
|
|
||
| ## Other things to consider | ||
|
|
||
| In most cases, the issues people face have to do with one of the above reasons listed and can be solved with the given suggestions. | ||
| However, if you are still running into issues, I’ll keep an ongoing list of other possibilities and things to check for. | ||
|
|
||
| - Disabled CORS in browser. (I’ve seen people have this issue with browser extensions) | ||
|
|
||
| ## Aditional resources | ||
| Visit our documentation, Discord server, YouTube channel, or Threads page to find more resources on this topic. | ||
|
|
||
| {% arrow_link href="https://appwrite.io/docs/advanced/security/abuse-protection/" %} | ||
| Learn more in our documentation | ||
| {% /arrow_link %} | ||
| {% arrow_link href="https://appwrite.io/discord" %} | ||
| Join the Appwrite Discord server | ||
| {% /arrow_link %} | ||
| {% arrow_link href="https://www.youtube.com/watch?v=oEpRh9H5l5g" %} | ||
| Follow the tutorial on YouTube | ||
| {% /arrow_link %} | ||
| {% arrow_link href="https://appwrite.io/threads" %} | ||
| Search past support threads | ||
| {% /arrow_link %} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add another line with resources: