def post(self): data = request.get_json() response = dict() response['status'] = 'failure' response['data'] = 'Some error occurred. Please try again.' session = DBBackend().get_session() try: user = session.query(User).filter_by( email=func.binary(data['email'])).one() response['status'] = 'success' response['data'] = dict() response['data']['user_id'] = user.id response['data']['email'] = user.email response['data']['auth_token'] = user.encode_token() session.add(user) session.commit() response_code = 200 except NoResultFound: response['data'] = 'User not found.' response_code = 401 except Exception as ex: response['data'] = "Error occured : " + str(ex.message) response_code = 500 response['status'] = 'failure' session.close() return make_response(jsonify(response)), response_code
def decorated(*args, **kwargs): auth_header = request.headers.get('Authorization') try: token = auth_header.split(" ")[1] decoded_token = User.decode_token(token) if not isinstance(decoded_token, str): g.user_id = decoded_token session = DBBackend().get_session() user = session.query(User).filter_by(id=g.user_id).one() session.close() return f(*args, **kwargs) else: return token_error(decoded_token) except IndexError: return token_error() except AttributeError: return token_error() except Exception as ex: return token_error(ex)