title: How to add change password functionality to Django / React app
body: <p>I'm trying to add change password functionality to a Django / React app. I'm struggling to find any way to make this work. I'm using django-rest-auth and django-rest-framework.</p>
  
  <p>I would be happy to either submit the password change request from a React page, or else to redirect the user to the native Django template-driven page - leaving my SPA, but acceptable to get the functionality working.</p>
  
  <p>However, the change password page requires the user to be logged in, and I can't see how to do this from the React app. I've set up the Django page with a custom template, but if I link to it from the app then the URL redirects to login:</p>
  
  <p>If I copy the request to cURL and run it in a terminal, I see this error:</p>
  
  <pre><code># api/urls.py
  from django.urls import include, path
  from django.contrib.auth import views
  from django.conf.urls import include, url
  
  urlpatterns = [
  ...
      path('password_change/', views.PasswordChangeView.as_view(template_name='account/password_change.html'), name='password_change'),
  ]
  </code></pre>
  
  <p>account/password_change.html</p>
  
  <pre><code>{% extends 'account/base.html' %}
  
  {% block body_block %}
    &lt;h1&gt;Change your password&lt;/h1&gt;
    &lt;form method="post" action="."&gt;
      {% csrf_token %}
      {{ form.as_p }}
      &lt;input type="submit" value="Submit" /&gt;
    &lt;/form&gt;
  {% endblock %}
  </code></pre>
  
  <p>I log in to my app and then navigate to where the change password page should be:</p>
  
  <pre><code>http://localhost:8000/api/v1/password_change/
  </code></pre>
  
  <p>This is then redirected to:</p>
  
  <pre><code>http://localhost:8000/accounts/login/?next=/api/v1/password_change/
  </code></pre>
  
  <p>My guess is that this is because the endpoint requires authentication and it's not been provided. But although I have a stored authentication token, I can't find out how to send this as part of the weblink and previous experience with reset password suggests that it's not the token Django would be looking for from a template.</p>
  
  <p>I've hunted for ages and can't find any example of how to add change password to a React app. Surely this must be something every React app needs? What basic thing am I missing?</p>
  
  <p>Edit: thanks to Rik Schoonbeek for the answer, which is that the endpoint supplied by djang-rest-auth can be used directly. Somehow I missed this in all my confusion. In case it helps anybody else, here is sample working code that uses JavaScript fetch and hard-coded passwords instead of data from the form, just to demonstrate the process.</p>
  
  <p>This code also demonstrates detecting an error in the fetch command, which doesn't work quite like you might expect. There are probably better ways to do it, but this does at least give you a hook.</p>
  
  <p>Working code:</p>
  
  <pre><code>export const changePassword = () =&gt; (dispatch) =&gt; {
      const token = localStorage.getItem('jwtToken');
  
      const data = {
          'new_password1': 'othertext',
          'new_password2': 'othertext',
          'old_password': 'sometext'
      };
  
      var formData  = new FormData();
  
      // Push our data into our FormData object
      for(var name in data) {
          formData.append(name, data[name]);
      }
  
      const headers = {
          'Authorization': `Token ${token}`,
      };
  
      return fetch('/api/v1/rest-auth/password/change/', {
          headers,
          'method': 'POST',
          'body': formData,
      })
          .then(res =&gt; {
              if(res.ok) {
                  console.log('successfully changed password');
                  return res.json();
              } else {
                  console.log('error changing password');
              }
          })
          .then(res =&gt; {
              console.log('Change password res ', res);
          });
  };
  </code></pre>
  
  <p>And important! add this to settings.py, otherwise the password will be changed regardless of whether the old password is correct:</p>
  
  <pre><code>OLD_PASSWORD_FIELD_ENABLED = True
  </code></pre>
  
  <p>And if you want the user to stay logged in after they have reset their password, add this too:</p>
  
  <pre><code>LOGOUT_ON_PASSWORD_CHANGE = False
  </code></pre>