I am building a Flask server and I want to check for the access key when sending the curl request. Atm I am adding @token_required before every method and this way works good. However if I send a wrong request or something else happens it return "wrong key" or "token is missing". Is there any other way to implement it.
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = ""
if 'x-access-token' in request.headers:
token = request.headers['x-access-token']
if not token:
return jsonify({'message':'token is missing'}), 401
try:
if token == app.config['SECRET_KEY']:
return f(*args, **kwargs)
else:
return jsonify("wrong key"), 401
except:
return jsonify({'message':'Token is invalid'}), 401
return decorated