I
I
Ivan Stroykin2016-11-24 17:26:45
Angular
Ivan Stroykin, 2016-11-24 17:26:45

How to properly work with csrftoken from django in angular2?

Good day,
How to properly work with csrftoken from django in angular2?
So far, so as not to interfere with development, I wrote a skeleton

document.cookie.replace(/(?:(?:^|.*;\s*)csrftoken\s*\=\s*([^;]*).*$)|^.*$/, "$1");

But after I added the angular2-cookie dependency, even this method became unavailable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Stroykin, 2016-11-29
@StivinKing

Solved the problem in general terms. True, it was not without a small crutch, but apparently there is no other way so far.
Here, either use the page that will be generated on django by the application for authentication, or when sending the form from the application to angular2, privately generate the form with the same django to correctly receive the token.
Well, thanks to the angular2-cookie package, the access to the token has been replaced with this:

document.cookie.replace(/(?:(?:^|.*;\s*)csrftoken\s*\=\s*([^;]*).*$)|^.*$/, "$1");

like this:
If someone has a better solution, I will be glad to know it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question