Skip to content

Instantly share code, notes, and snippets.

@pharzan
Last active April 23, 2018 13:28
Show Gist options
  • Save pharzan/ee5b78299ed5e7e9bbe9fad95ff715d7 to your computer and use it in GitHub Desktop.
Save pharzan/ee5b78299ed5e7e9bbe9fad95ff715d7 to your computer and use it in GitHub Desktop.

To create restframe work login in django:

https://michaelwashburnjr.com/django-user-authentication/ http://polyglot.ninja/django-rest-framework-json-web-tokens-jwt/

    // AJAX Request::
   JWToken = window.sessionStorage.accessToken;
                        console.log(JWToken)
                        if (JWToken !== undefined) {
                            m.request({
                                    method: "POST",
                                    url: "http://127.0.0.1:8000/api/clients/",
                                    // headers: { 'X-CSRFToken': csrftoken },
                                    headers: { 'Authorization': "JWT " + JWToken },
                                    // 'X-CSRFToken':,
                                    data: { id: this.value },
                                    // withCredentials: true,
                                })
// Get Token and store it:
                    m.request({
                                method: "POST",
                                url: "http://127.0.0.1:8000/api/jwt-auth/",
                                headers: { 'X-CSRFToken': csrftoken },
                                // 'X-CSRFToken':,
                                data: {
                                    'username': login.vm.username,
                                    'password': login.vm.password
                                },
                                // withCredentials: true,
                            }).then(function(data) {
                                // count = parseInt(data)
                                console.log(data)
                                window.sessionStorage.accessToken = data['token'];
                            })
                            

import datetime
JWT_AUTH = {
    'JWT_EXPIRATION_DELTA': datetime.timedelta(hours=1),
    'JWT_ALLOW_REFRESH': True,
}

REST_FRAMEWORK = {
    # 'DEFAULT_AUTHENTICATION_CLASSES': (
    #     'rest_framework.authentication.BasicAuthentication',
    #     'rest_framework.authentication.SessionAuthentication',
    # )

      'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
           'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ),

}
REST_USE_JWT = True

##To check SITE_ID and add to settings.py incase /admin doesn't work

# python manage.py shell
>>> new_site = Site.objects.create(domain='foo.com', name='foo.com')
>>> print(new_site.id)

``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment