I'm trying to build a token based backend (API) for an app using Flask in which I'm trying to use Flask_Security. Since I'm using the Peewee ORM, I've followed this guide to build the basic setup and I now have to build the views which should login the user and then a view which actually serves some useful data.
So my login view which returns the token looks like this:
@app.route('/api/login', methods=['POST'])
def api_login():
requestJson = request.get_json(force=True)
user = User.select().where(User.username == requestJson['username']).where(User.password == requestJson['password']).first()
if user:
return jsonify({'token': user.get_auth_token()})
else:
return jsonify({'error': 'LoginError'})
This works fine; I get a token as a response. I now want to protect another view using auth_token_required
and I want to use the token as a header. So I try this as follows:
@app.route('/api/really-important-info')
@auth_token_required('SECURITY_TOKEN_AUTHENTICATION_HEADER')
def api_important_info():
return jsonify({'info': 'really important'})
But starting Flask results in an AttributeError: 'str' object has no attribute '__module__'
. The documentation isn't very helpful on its usage either.
Does anybody know how I can get this to work? Any tips are welcome!