Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use login_required with a class, in Flask?

from flask_login import login_required
from flask_restful import Resource

@login required
class MyClass(Resource):
    #...

In the main file I call the class's methods like:

api.add_resource(MyClass, '/some_url', methods=['GET', 'PUT', 'POST', 'DELETE'])

I think I am using @login_required the wrong way here, since I get the error AttributeError: 'function' object has no attribute 'as_view'

So I am assuiming that @login_required can only be used with functions. Is there a way to incorporate it with a class?

like image 236
Snow Avatar asked Oct 27 '25 22:10

Snow


1 Answers

you could give the class the decorators it should run :

class MyClass(Resource):
    decorators = [login_required]
like image 169
senaps Avatar answered Oct 29 '25 13:10

senaps



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!