title: Flask authentication and blueprints
body: <p>I'm trying to protect my pages using basic login_required method. This is working fine for main page (dashboard) but not for loaded blueprints. Any way of protecting them too?
  I'd like to keep my app modular rather than having all my code in main app.</p>
  
  <p>I have the following:</p>
  
  <pre><code>app = flask.Flask(__name__)
  app.secret_key = 'T0pS3cr3tKeY!'
  
  def login_required(f):
      @wraps(f)
      def wrap(*args, **kwargs):
          if 'logged_in' in flask.session:
              return f(*args, **kwargs)
          else:
              flask.flash('You need to login first!')
              return flask.redirect(flask.url_for('/'))
      return wrap
  
  
  @app.route('/', methods=['GET', 'POST'])
  def login():
      error = None
      if flask.request.method == 'POST':
          if flask.request.form['email'] != 'admin' or flask.request.form['password'] != 'admin':
              error = 'Invalid login details, please try again.'
          else:
              flask.session['logged_in'] = True
              flask.flash('You were just logged in!')
              return flask.redirect(flask.url_for('dashboard'))
      return flask.render_template('login.html', error = error)
  
  
  @app.route('/dashboard')
  @login_required
  def dashboard():
      return flask.render_template('index.html')
  
  
  @app.route('/logout')
  def logout():
      flask.session.pop('logged_in', None)
      flask.flash('You were just logged out!')
      return flask.redirect(flask.url_for('login'))
  
  
  app.register_blueprint(app1)
  app.register_blueprint(app2)
  app.register_blueprint(app3)
  </code></pre>