Search code examples
javascriptdjangodjango-rest-frameworkdjango-sessions

Get session ID for a django site in javascript


How can I get the django session ID that is ultimately stored in the cookie using javascript?

The reason I need to get that ID is that I'm doing everything from a REST API, including authentication. So, the guys developing the mobile app are using ionic and they want to make a request to the API to get that session ID - and then use it in future requests. Any advice?


Solution

  • Hey i think this approach is not the best one, your backend is having a door here for XSS attacks , i think the best you can do its use a token for authentication even normal token.auth that is well explained on django-rest-framework docs http://www.django-rest-framework.org/api-guide/authentication/ but i use a lot JWT JSONweb token auth so u dont compromise other data than token, username or other custom stuff like roles or something your app logic have, since ionic its angular JS you can take a look into http://frederiknakstad.com/2013/01/21/authentication-in-single-page-applications-with-angular-js/ to see how you can manage the authentication process on frontend mobile app.