Django Rest Framework + React - token vs session authentication Django Rest Framework + React - token vs session authentication reactjs reactjs

Django Rest Framework + React - token vs session authentication


SessionAuthentication would be the most straightforward way to achieve what you want. The configuration is described at http://www.django-rest-framework.org/api-guide/authentication/#setting-the-authentication-scheme This will set a session id in a cookie that is handled by the browser.

Alternatively you can store a token in the browser's cookie but that is vulnerable to XSS attacks and other javascript attacks. For more security you can store the token in an HttpOnly cookie. The cookie would be persisted across tab/window closes by the browser.

Also to clarify cookie handling is built into most browsers. Your react state is in userland and lives in a different memoryspace than cookie storage.

More info:

  1. Ask HN: Cookies vs. JWT vs. OAuth
  2. https://developer.okta.com/blog/2017/08/17/why-jwts-suck-as-session-tokens
  3. http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/