Created
July 14, 2021 12:05
-
-
Save cadd/85b57146b956f0a9c030635dd76e7922 to your computer and use it in GitHub Desktop.
Get Auth-Header Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserAPI(MethodView): | |
""" | |
User Resource | |
""" | |
def get(self): | |
# get the auth token | |
auth_header = request.headers.get('Authorization') | |
if auth_header: | |
auth_token = auth_header.split(" ")[1] | |
else: | |
auth_token = '' | |
if auth_token: | |
resp = User.decode_auth_token(auth_token) | |
if not isinstance(resp, str): | |
user = User.query.filter_by(id=resp).first() | |
responseObject = { | |
'status': 'success', | |
'data': { | |
'user_id': user.id, | |
'email': user.email, | |
'admin': user.admin, | |
'registered_on': user.registered_on | |
} | |
} | |
return make_response(jsonify(responseObject)), 200 | |
responseObject = { | |
'status': 'fail', | |
'message': resp | |
} | |
return make_response(jsonify(responseObject)), 401 | |
else: | |
responseObject = { | |
'status': 'fail', | |
'message': 'Provide a valid auth token.' | |
} | |
return make_response(jsonify(responseObject)), 401 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment