2

I have an app, which is quite big, so it's divided into modules and use blueprints, add_url_rule for routing to various modules. Everything works fine and as intented.

The problem is, I need to use the Flask-Login (https://flask-login.readthedocs.org/en/latest/#api-documentation), from the documentaion. I could only find how to protect views using decorators, for which the app should have routes set using the app.route('/home'). But like I already mentioned, I'm using blueprints and add_url_rule.

EDIT: The answer @Mark Hildreth edited into this question, is the required solution. Thank you.

KBN
  • 2,915
  • 16
  • 27
  • @AjKumar There's not much I could try, but I did try doing `login_required(func)` inside a loop, where `func` is the `view function` of each module. It didn't do anything, no errors either. – KBN Oct 17 '13 at 12:53
  • maybe this will help you. http://stackoverflow.com/questions/16273499/flask-login-can-not-be-used-in-blueprint-object – ajknzhol Oct 17 '13 at 12:54
  • @AjKumar Yes, I did see that post prior to posting my question. The suggested solution in the answer is deprecated. And, it's just the main `app` variable. I have multiple modules, eg: Add, View, Edit etc, all registered as different blueprints. – KBN Oct 17 '13 at 13:05
  • 1
    Can you share at least 1 example of your blueprint view and inits ? You should still be able to use blueprint and flask-login. – codegeek Oct 17 '13 at 14:36
  • Where in the documentation does it say that "the app should have routes set using the `app.route('/home')`"? They probably just used that as the example because that's how a simple Flask app works. Have you tried putting the decorator on the blueprint's view function? As @codegeek asked, can you show this attempt? – Mark Hildreth Oct 17 '13 at 18:16

1 Answers1

3

This example with decorators:

@app.route('/')
@login_required
def index():
    pass

is equivalent to this example without decorators:

def index():
    pass
app.add_url_rule('/', 'index', login_required(index))
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152