title: Why isn't my current_user authenticated in flask-login?
body: <p>My goal is to make my home view (<code>/</code>) a login page. Once the user logs in, a different page is render depending on its role. When I login (<code>/auth</code>), I see that the username and password are correctly entered. It then attempts to render <code>/</code>, where it tells me that my user is not authenticated and renders <code>/login</code>. Here are the views that describe this:</p>
  
  <h1>Views</h1>
  
  <pre><code>@app.route("/login")
  def login():
      return flask.render_template('login.html')
  
  @app.route("/", methods=["GET"])
  def home():
      if current_user.is_authenticated:
          if current_user.is_admin():
              return flask.render_template('admin_index.html')
          return flask.render_template('user_index.html')
      logger.info("Not authenticated. Going back to login.")
      return flask.render_template('login.html')
  
  
  @app.route("/auth", methods=["POST"])
  def auth():
      username = request.form['username']
      password = request.form['password']
      user = db.session.query(User).filter(User.username == username).first()
      logger.info(user)
      logger.info("{0}: {1}".format(username, password))
      print("user exists? {0}".format(str(user != None)))
      print("password is correct? " + str(user.check_password(password)))
      if user and user.check_password(password):
          user.is_authenticated = True
          login_user(user)
          return flask.redirect(url_for('home'))
      return flask.redirect(url_for('login'))
  </code></pre>
  
  <p>The problem is that flask-login's <code>current_user.is_authenticated</code> is always returning <code>False</code> after I attempt to login. My created user is correctly created and committed to the database. Below is my User model with the necessary methods as per flask-login:</p>
  
  <h1>User model</h1>
  
  <pre><code>class User(db.Model):
      """
      A user. More later.
      """
  
      __tablename__ = 'User'
      id = db.Column(db.Integer, primary_key=True)
      username = db.Column(db.String(128), unique=True)
      hashed_password = db.Column(db.String(160))
      admin = db.Column(db.Boolean)
  
      def __init__(self, username, password="changeme123", admin=False):
          self.username = username
          self.set_password(password)
          self.admin = admin
          self.is_authenticated = False
  
      def is_active(self):
          return True
  
      def is_authenticated(self):
          return self.is_authenticated
  
      def is_anonymous(self):
          return False
  
      def is_admin(self):
          return self.admin
  
      def get_id(self):
          return self.id
  
      def __repr__(self):
          return '&lt;User {0}&gt;'.format(self.username)
  
      def set_password(self, password):
          self.hashed_password = generate_password_hash(password)
  
      def check_password(self, password):
          return check_password_hash(self.hashed_password, password)
  </code></pre>
  
  <p>Here is the <code>load_user</code> function:</p>
  
  <h1>load_user</h1>
  
  <pre><code>@login_manager.user_loader
  def load_user(user_id):
      try:
          return User.query.get(User.id==user_id)
      except:
          return None
  </code></pre>
  
  <hr>
  
  <p>Why is <code>current_user.is_authenticated</code> returning <code>False</code>? I presumed that <code>login_user(user)</code> would make <code>current_user == user</code>, i.e., the one who is being authenticated in <code>/auth</code>, but it seems this is not the case.</p>