I only want logged-in users to access my RESTfull API. I have been searching all over and couldn't find any source which tells me how to do it. Weird, because I think it's very common to protect data.
I am using a Flask
project with Flask-login
and flask-Restless
. I CRUD my data through SQL-alchemy
classes to access my MySQL database. I create my RESTfull api like:
api_manager = APIManager(app, flask_sqlalchemy_db=db)
api_manager.create_api(Items, methods=['GET', 'POST', 'DELETE', 'PUT'])
How should I restrict access to my RESTfull api for users who are not logged in, or can't I do it with flask-restless? If not, what should/could I better use?
I am trying out some techniques, so suggestions in any directions are welcome!
Thanks in advance
After some more playing around I have found a solution. It might not be the best but it does the trick without too much code:
@app.before_request
def before_request():
if ('/api/' in str(request.url_rule)) && (not current_user.is_authenticated()):
return redirect('/login')
Is this the right way to go? Adding Preprosessors is a lot of code for every possible HTTP request. https://flask-restless.readthedocs.org/en/latest/customizing.html#request-preprocessors-and-postprocessors