def post_user_account(): payload = g.payload print(payload) user = User.query.filter_by(id=payload.get('id')).first() if user is None: return make_json_response({"status": "error", "message": "JWT error. User must re-authenticate."}, 400) if request.json.get('new_password') != request.json.get('confirm_password'): return make_json_response({"status": "error", "message": "Passwords don't match!"}, 400) user.update_password(request.json.get('new_password')) commit_to_db(user) return make_json_response({"status": "success", "message": "Account successfully updated"}, 200)
def wrap(*args, **kwargs): if "Authorization" not in request.headers.keys(): return make_json_response({"status": "error", "body": "Missing JWT authorization"}, 401) payload = verify_token(request.headers.get("Authorization"), request.remote_addr) if not payload["success"]: return make_json_response({"status": "error", "body": "Unauthorized"}, 401) g.payload = payload["payload"] return f(*args, **kwargs)
def register_user(): user_exist = User.query.filter_by(email=request.json["email"]).first() if user_exist is not None: return make_json_response({ "status": "error", "body": f"user with email ({request.json['email']}) already exists"}, 400) if request.json.get('password') != request.json.get('confirm_password'): return make_json_response({ "status": "error", "message": "Passwords don't match!" }, 400) user = User(request.json["email"], request.json["password"]) commit_to_db(user) return make_json_response({ "status": "success", "body": "user registered", "token": assign_token(user.to_dict(), request.remote_addr)} , 201)
def login_user(): user = User.query.filter_by(email=request.json["email"]).first() if user is None: return make_json_response({ "status": "error", "message": "Incorrect user information" }, 400) if not check_password(request.json["password"], user.password): return make_json_response({ "status": "error", "message": "Incorrect user information" }, 400) return make_json_response({"status": "success", "message": "user authenticated", "token": assign_token(user.to_dict(), request.remote_addr)} , 200)
def get_user_account(): payload = g.payload return make_json_response(payload, 200)
def bad_json(): return make_json_response({"status": "error", "body": "incorrect request JSON"}, 400)
def missing_json(): return make_json_response({"status": "error", "body": "JSON missing"}, 400)
def database_reset(): reset_database() return make_json_response({"status": "success", "body": "DB Reset"}, 200)