Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django cookies are not getting saved on browser

I am making an application with react and Django. When i login with django i set token in cookie but Django response cookies are not set in browser. I trying to debug it hard but could not. Don't know where i m doing wrong.

Request URL:http://localhost:8000/login/
Request Method:POST
Status Code:200 OK
Remote Address:127.0.0.1:8000

Response Headers
    Access-Control-Allow-Credentials:true
    Access-Control-Allow-Origin:http://localhost:3000
    Content-Type:application/json
    Date:Sun, 12 Feb 2017 13:32:30 GMT
    Server:WSGIServer/0.1 Python/2.7.10
    Set-Cookie:token=97547ba32cb8abcfe81b28c47ee5e3b8087b54ac; Path=/
    Set-Cookie:sessionid=6twm2h9mad6q8k4w637ww25zt6l0ck2d; expires=Sun, 26-Feb-2017 13:32:30 GMT; httponly; Max-Age=1209600; Path=/
    Vary:Cookie
    X-Frame-Options:SAMEORIGIN

Request Headers
    Accept:application/json, text/plain, */*
    Accept-Encoding:gzip, deflate, br
    Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
    Authorization:Token undefined
    Connection:keep-alive
    Content-Length:41
    Content-Type:application/json;charset=UTF-8
    Host:localhost:8000
    Origin:http://localhost:3000
    Referer:http://localhost:3000/
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

client Request Code

export function adminSignIn({username, password}) {
    return function (dispatch) {
        axios
            .post("http://localhost:8000/login/", {username, password})
            .then((response) => {
                dispatch({type: ADMIN, payload: response.data})
                dispatch({type: HIDE_LOGGIN_MODAL, payload: response.data})
            })
            .catch((error) => {
                console.log(error)
            })
    }
}

settings.py

"""
Django settings for server project.

Generated by 'django-admin startproject' using Django 1.8.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '83vwkomf5s1y_!jt#=_dlgv!v0t38yl!a80h#r0buor70$0y7='

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'corsheaders',
    'inventory',
    'home'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 40
}

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True

# CSRF_COOKIE_NAME = "XSRF-TOKEN"

ROOT_URLCONF = 'server.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'server.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'bigbasket',
        'USER': 'naresh',
        'PASSWORD': 'naresh',
        'HOST': 'localhost'
        # 'PORT': '',
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

There is no cookie saved in browser after completion of this request. Need some help here.

like image 655
Naresh Avatar asked Feb 12 '17 13:02

Naresh


2 Answers

Found Solution was not sending withCredentials: true Thanks @sideshowbarker

like image 53
Naresh Avatar answered Sep 22 '22 15:09

Naresh


in Axios Post Add axios.post('http://example.com',null,{withCredentials: true})

like image 44
E-RROR-E Avatar answered Sep 22 '22 15:09

E-RROR-E