def inner(*args, **kwargs): identity = get_jwt_identity() user_handler = UserHandler() user = user_handler.user_table.find_one({"id": identity["id"]}, {"_id": 0}) if not user: response = { "error": "Authentication token incorrect. Try logging in again." } return make_response(jsonify(response), 403) return func(*args, **kwargs)
def patch(self): """Logout user. """ identity = get_jwt_identity() user_handler = UserHandler() user = user_handler.user_table.find_one({"id": identity["id"]}) if not user: response = {"error": "User not found. Couldn't logout."} return make_response(jsonify(response), 404) jwt_token = request.environ.get("HTTP_AUTHORIZATION").split(" ")[-1] user_handler.blacklist_token({"id": identity["id"]}, jwt_token) response = {"message": "Logged out successfully."} return make_response(jsonify(response), 200)
def get(self): """Fetch details of logged in user. Returns: A user object. """ identity = get_jwt_identity() user_handler = UserHandler() user = user_handler.user_table.find_one({"id": identity["id"]}, {"_id": 0}) print(user) if not user: response = { "error": "User not found or authentication token has expired. Try login again." } return make_response(jsonify(response), 404) return make_response(jsonify({"data": user}), 200)
def post(self): """Registers a user. Returns: Details of user and access token. """ data = json.loads(request.get_data()) user_handler = UserHandler() user_id, additional_id = user_handler.create_user(data) identity = {"id": user_id, "additionalId": additional_id} response = { "data": { "token": create_access_token(payload=identity, expires_delta=False), "user_id": user_id, } } return make_response(jsonify(response), 201)
import falcon from handlers import UserHandler, FindNeighborsHandler from middlewares import MiddlewareSessionManager from errors import ApplicationError, exception_handler from utils.db import SessionManager from utils.config import get_config # middleware api = falcon.API(middleware=[ MiddlewareSessionManager(session_manager=SessionManager( config=get_config()['db'])) ]) api.add_error_handler(ApplicationError, exception_handler) # routes api.add_route('/user', UserHandler()) api.add_route('/neighbors/{coords}', FindNeighborsHandler())