Search code examples
pythonflaskflask-security

How could you refactor current_user in User.query.all() in Flask-Security?


Index page should be shown only to logged in users and redirect other users to landing page.

@app.route('/')
def index():
    if current_user in User.query.all():
        return render_template('index.html')
    else:
        return render_template('landing.html')

So how could you refactor current_user in User.query.all() part? Should I customize the @login_required somehow? How have others dealt with this problem?


Solution

  • Use current_user.is_authenticated property. e.g

    if current_user.is_authenticated:
        return render_template('index.html')
    else:
        return render_template('landing.html')