title: Flask-User @login_required and @roles_required not working
body: <p>I am trying to use Flask-User (which incorporates Flask-Login). I would like to use the <code>@login_required</code> and <code>@roles_required</code> decorators to manage access to routes. However, it is not working.</p>
  
  <p>The Flask-User documentation shows <code>login_required</code> being imported from <code>flask_user</code>, not <code>flask_login</code>. When imported from <code>flask_login</code> it works as expected, but if I switch it to be imported from <code>flask_user</code> it does not work. Instead, it says that an authenticated user is not signed in.</p>
  
  <p>Why don't I just import from <code>flask_login</code>? Well, I could, but that does not solve my other problem which is that <code>@roles_required</code> can only be imported from <code>flask_user</code>, and that does not work either (same problem as above - says that authenticated users are not signed in).</p>
  
  <p>My setup is below. I would be grateful if anyone could help me out, please. I have previously had Flask User working on another app, but can't figure out what's going wrong here. At the moment, the user database is stored in an SQLite database.</p>
  
  <p>config.py</p>
  
  <pre><code># Flask-User settings
      USER_APP_NAME = "App name"      # Shown in and email templates and page footers
      USER_ENABLE_EMAIL = True        # Enable email authentication
      USER_ENABLE_USERNAME = False    # Disable username authentication
      USER_EMAIL_SENDER_NAME = USER_APP_NAME
      USER_EMAIL_SENDER_EMAIL = "email"
      USER_ENABLE_AUTH0 = True
  
      USER_UNAUTHENTICATED_ENDPOINT = "login"
      USER_UNAUTHORIZED_ENDPOINT = "login"
  </code></pre>
  
  <p>__init__.py</p>
  
  <pre><code># Initialise flask_login
  login = LoginManager(app)
  login.login_view = 'login'
  login.refresh_view = "login"
  login.needs_refresh_message = "Please sign in again to access this page"
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>from andon import db, app, login
  from flask_user import UserMixin, UserManager
  from flask_login import current_user
  
  @login.user_loader
  def load_user(id):
      return User.query.get(int(id))
  
  class User(db.Model, UserMixin):
      id = db.Column(db.Integer, primary_key=True)
  
      # User Authentication fields
      email = db.Column(db.String(255), nullable=False, unique=True)
      email_confirmed_at = db.Column(db.DateTime())
      password = db.Column(db.String(255), nullable=False)
  
      # User fields
      active = db.Column(db.Boolean())
      first_name = db.Column(db.String(50), nullable=False)
      last_name = db.Column(db.String(50), nullable=False)
      registered = db.Column(db.DateTime, default=datetime.utcnow)
      last_login = db.Column(db.DateTime)
      client_id = db.Column(db.Integer, db.ForeignKey('client.id', ondelete='CASCADE'))
      login_count = db.Column(db.Integer, default=0)
      invitation_token = db.Column(db.String(50))
  
      roles = db.relationship('Roles', secondary='user_roles')
  
      def check_password(self, password):
          # Check password against hash; return True or False
          return bcrypt.checkpw(password.encode('utf8'), self.password)
  
  # Define the Role data model
  class Roles(db.Model):
      id = db.Column(db.Integer(), primary_key=True)
      name = db.Column(db.String(50), nullable=False, unique=True)  # for @roles_accepted()
  
      def __repr__(self):
          return self.name
  
  
  # Define the UserRoles association model
  class UserRole(db.Model):
      __tablename__ = 'user_roles'
      id = db.Column(db.Integer(), primary_key=True)
      user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE'))
      role_id = db.Column(db.Integer(), db.ForeignKey('roles.id', ondelete='CASCADE'))
  
  # Setup Flask-User
  user_manager = UserManager(app, db, User)
  
  </code></pre>
  
  <p>main.py</p>
  
  <pre><code>from flask_login import current_user, login_user, logout_user
  from flask_user import roles_required, login_required
  
  @app.route('/test')
  @login_required
  def register_hub():
  
      return "Test"
  </code></pre>