@login_required trouble in flask app

When we want the user not to access the private page or the page which requires login for that case flask provides decorators.

@app.route("/welcome")
@login_required  # If the user is not logged in then it will redirected to unauthorized_handler
def welcome_page():
     return """<h1> welcome user</h1>"""

@login_manager.unauthorized_handler     # In unauthorized_handler we have a callback URL 
def unauthorized_callback():            # In call back url we can specify where we want to 
       return redirect(url_for('login')) # redirect the user in my case it is login page!

I hope your problem is solved !!!


You have to change the order of the decorators. Quoting the Flask documentation:

So how would you use that decorator now? Apply it as innermost decorator to a view function. When applying further decorators, always remember that the route() decorator is the outermost:

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