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)
``