title: Error with flask-login
body: <p>I have difficulty in using the Flask-Login framework for authentication. I have looked through the documentation as thoroughly as possible but apparently I am missing something obvious.</p>
  
  <pre><code>class User():
      def __init__(self, userid=None, username=None, password=None):
          self.userid = userid
          self.username = username
          self.password = password
  
      def is_authenticated(self):
          return True
  
      def is_active(self):
          return True
  
      def is_anonymous(self):
          return False
  
      def get_id(self):
          return unicode(self.userid)
  
      def __repr__(self):
          return '&lt;User %r&gt;' % self.username
  
  def find_by_username(username):
      try:
          data = app.mongo.db.users.find_one_or_404({'username': username})
  
          user = User()
          user.userid = data['_id']
          user.username = data['username']
          user.password = data['password']
          return user
  
      except HTTPException:
          return None
  
  
  def find_by_id(userid):
      try:
          data = app.mongo.db.users.find_one_or_404({'_id': userid})
          user = User(data['_id'], data['username'], data['password'])
          return user
  
      except HTTPException:
          return None
  </code></pre>
  
  <p>The above is my User class located in <code>users/models.py</code></p>
  
  <pre><code>login_manager = LoginManager()
  login_manager.init_app(app)
  login_manager.login_view = 'users.login'
  
  @login_manager.user_loader
  def load_user(userid):
  return find_by_id(userid)
  </code></pre>
  
  <p>The above is my user loader.</p>
  
  <pre><code>@mod.route('/login/', methods=['GET', 'POST'])
  def login():
      form = LoginForm()
      if form.validate_on_submit():
          pw_hash = hashlib.md5(form.password.data).hexdigest()
          user = find_by_username(form.username.data)
          if user is not None:
              if user.password == pw_hash:
                  if login_user(user):
                      flash('Logged in successfully.')
                      return redirect(request.args.get('next') or url_for('users.test'))
                  else:
                      flash('Error')
  
          else:
              flash('Username or password incorrect')
  
      else:
          flash('Username or password incorrect')
  
  return render_template('users/login.html', form=form)
  </code></pre>
  
  <p>There is no apparently error message, but when trying to access any views decorated with <code>@login_required</code>, it redirects me to the login form. Best as I can tell, the <code>login_user</code> function isn't actually working although it returns <code>True</code> when I called it. Any advice appreciated.</p>