Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify Flask-Login @Login_Required Decorator

I would like to change the default Flask-Login @login_required so that it will check a database as to whether a user is banned or not.

I am aware I could create a custom decorator to check my database and add it to all my views, however this would be less useful due to the number of views I have; it wouldn't be convenient.

Is this possible or not? Thanks.

like image 316
Pav Sidhu Avatar asked Sep 08 '25 15:09

Pav Sidhu


1 Answers

You can use the next approach:

from flask.ext.login import LoginManager

@login_manager.user_loader
def load_user_if_not_banned(user_id):        
    user = ... # Load user from DB
    return user if not user.is_banned else None
like image 154
Ivan Velichko Avatar answered Sep 10 '25 07:09

Ivan Velichko