Getting Started
Local Development
When developing locally with OLAF, you may encounter CORS (Cross-Origin Resource Sharing) issues. This guide explains what CORS is, why it can be problematic during local development, and how to resolve it by properly configuring your environment.
What is CORS?
CORS (Cross-Origin Resource Sharing) is a browser security mechanism that restricts how resources on a web page can be requested from a different domain or origin. It ensures that only authorized domains can access specific resources on a server.
For example:
- If your app is running on http://localhost:3000 and tries to call an OLAF API hosted on https://accounts.olaf.sh, the browser will block the request unless OLAF explicitly allows it.
Why is CORS an Issue in Local Development?
When using OLAF for authentication or API integration during local development:
- Your app’s local domain (e.g., http://localhost:3000) is not automatically allowed to access OLAF’s resources.
- OLAF only allows requests from trusted subdomains that you define in the Callback URL configuration of your app.
- This restriction prevents unauthorized access but can block requests during local testing if your system is not configured to recognize your assigned OLAF subdomain.
How to Solve This
Instead of adding localhost to OLAF, you will:
- Copy the Hostname value from Hosts tab on the Application details page.
- E.g.
<app-name>-<account-name>.apps.olaf.sh
- E.g.
- Map copied Hostname value to 127.0.0.1 in your system’s hosts file for local development.
This setup ensures that OLAF recognizes your app’s host and avoids CORS issues.
On macOS or Linux
- Open the
/etc/hosts
file with a text editor with administrator permissions:sudo nano /etc/hosts
- Add the following entry, replacing
<app-hostname>
with your Hostname value:127.0.0.1 <app-hostname>
- Save the file and exit.
On Windows
- Open Notepad as an administrator.
- Navigate to the hosts file:
C:\Windows\System32\drivers\etc\hosts
- Add the following entry, replacing
<app-hostname>
with your Hostname value:127.0.0.1 <app-hostname>
- Save the file and exit.