Django-Rest-Knox provides a view to give you the token to authenticate, but that view requires authentication:
How is this intended to be used?
Following the documentation on setting it up, my settings look like this:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': ('knox.auth.TokenAuthentication',),
}
I tried authenticating this way:
fetch("http://localhost:8000/api-v1/auth/login/", {
method: "POST",
body: JSON.stringify({email: email, password: password}),
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
}
}).then(result => result.json())
.then(result => {
console.log(result);
});
but IsAuthenticated
is stopping me and showing this message:
Unauthorized: /api-v1/auth/login/
on the Django side, and this on the JavaScript side:
{"detail":"Authentication credentials were not provided."}
Email and password are the same I'm using to log in successfully on the admin tool.
Just for clarification, I don't have a system where someone logs in to a web application and copies and pastes a token to some other application. In the system I'm building, you log in directly through the API with your user and password, then the token is obtained and saved for subsequent requests.
Very similar to how LastPass works, that the first time you open it, it asks you for an email and password (not to go away and fetch a token from a web site):
The whole objective of Knox's LoginView is to generate and provide that token in an API request: https://github.com/James1345/django-rest-knox/blob/05f218f1922999d1be76753076cf8af78f134e02/knox/views.py#L30-L55
This is also how pretty much every mobile app as well as SPAs work, in which they ask you for a user/email and password to obtain the token, and then save the token for further requests.