Flask: decorate each route at once?

I have a @login_required decorator that decorates a controller action. However, my application is very large and has many routes in many controller files. Going one by one to decorating each route seems error prone (I could easily skip one) and a lot of time.

Is there a way to decorate all the routes at once throughout the application?

I am moving authentication from the web server (apache) to the application tier, so I have this problem.

+5
source share
1 answer

You can go the opposite way and use the before_request decorator to require default login and use your own decorator for route tags that do not require login, for example:

 _insecure_views = [] @my_blueprint.before_request def require_login(): if request.endpoint in _insecure_views: return # check for login here def login_not_required(fn): '''decorator to disable user authentication''' endpoint = ".".join([some_blueprint.name, fn.func_name]) _insecure_views.append(endpoint) return fn @some_blueprint.route('/') @login_not_required def index(): pass 

You could probably transfer this to your own production plan / class of flacks.

Edit: basically the best way to make the default-login login_required checkbox

+8
source

Source: https://habr.com/ru/post/1237684/


All Articles