title: Flask-Login, Session Management and AJAX
body: <p>I'm having trouble getting an AJAX call to work with the Flask-Login <code>current_user</code> object and the <code>@login_required</code> decorator.  I believe it has something to do with the session not being passed.  </p>
  
  <p>The goal is for a user to click on a link that makes an AJAX call to a route URL that will provide a SSO session ID through a SOAP service.  The SOAP service requires an emp_id to be passed which can be accessed through the <code>current_user.emp_id</code> attribute.</p>
  
  <p>I initially attempted the AJAX call without the <code>@login_required</code> decorator and just passing a simple message as such which returned successfully:</p>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>const ssoLinks = document.querySelectorAll('.sso');
  
  ssoLinks.forEach((ssoLink) =&gt; {
    ssoLink.addEventListener('click', (e) =&gt; generateSSOLink(e, ssoLink.id));
  });
  
  
  function generateSSOLink(e, page) {
    e.preventDefault();
  
    fetch("/get_bpm_sso_link")
      .then(response =&gt; response.json())
      .then(data =&gt; console.log(data))
  }
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>@app.route('/get_bpm_sso_link')
  def get_bpm_sso_link():
      data = {
          'msg': 'success'
      }
  
      print('*'*75)
      print('SUCCESS')
      print('*'*75)
  
      return jsonify(data)
  </code></pre>
  
  <p>My next attempt was to access the emp_id in <code>current_user</code>:</p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>@app.route('/get_bpm_sso_link')
  def get_bpm_sso_link():
      data = {
          'msg': 'success'
      }
  
      print('*'*75)
      print(current_user.emp_id)
      print('*'*75)
  
      return jsonify(data)
  </code></pre>
  
  <p>Which gave me this error:</p>
  
  <blockquote>
    <p>AttributeError: 'AnonymousUserMixin' object has no attribute 'emp_id'</p>
  </blockquote>
  
  <p>Okay, so then I decided to try and access the same emp_id attribute on the initial index page(where the AJAX lives as well) and was able to do so.  It sounds like during an AJAX request, the Flask-Login LoginManager doesn't recognize the session of the <code>current_user</code>.  To further this claim, I added the <code>@login_required</code> decorator to the route and the JSON response returned a redirect to the login page.</p>
  
  <p>What can I do to have Flask-Login recognize a user during an AJAX call?  If not, what other libraries can handle user session management and AJAX requests seamlessly?</p>