Im working on a DRF (Django project) where my backend django rest api is hosted on a server and my ReactJS frontend is also hosted on the same server. I had made sure to follow all the steps needed as what I've read in the ff documentations:
- https://github.com/adamchainz/django-cors-headers
- http://www.srikanthtechnologies.com/blog/python/enable_cors_for_django.aspx
I have added corsheaders INSTALLED_APPS and my middleware in settings.py is:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
And my CORS Settings in settings.py is:
CORS_ALLOW_ALL_ORIGINS=False
CSRF_TRUSTED_ORIGINS = [
"https://samplefrontend.tech",
]
CORS_ALLOW_METHODS = [
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
]
CORS_ALLOW_HEADERS = [
'accept',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
]
However, when I try to do some requests using Postman from my local PC (not from the frontend server), example, get token, the rest api returns the refresh and access tokens. This also holds true with other HTTP requests, I can freely perform these requests using postman and get a response.
What I needed is that only requests coming from the frontend app (reactjs) should be accepted. Can anyone help me on this?