async def create_user(user: UserInDB): if db.users.find({'username': user.username}).count() > 0: raise HTTPException(status_code=409, detail="User exists") pwd = get_password_hash(user.hashed_password) user.hashed_password = pwd ret = db.users.insert_one(user.dict(by_alias=True)) user.id = ret.inserted_id return {'user': user}
async def register_user(request: RegisterRequest, client: AsyncIOMotorClient = Depends(get_nosql_db)): try: collection = client[getenv("MONGODB_NAME")]["user"] user = create_user(request) dbuser = UserInDB(**user.dict()) response = await collection.insert_one(dbuser.dict()) return {"user_id": str(response.inserted_id)} except DuplicateKeyError as e: print(e) return JSONResponse(status_code=400, content={"user_id": "user_alreay exists"})
async def create_user(request, collection): salt = uuid.uuid4().hex hashed_password = hashlib.sha512( request.password.encode("utf-8") + salt.encode("utf-8")).hexdigest() user = {} user["username"] = request.username user["salt"] = salt user["hashed_password"] = hashed_password # user = User(**user) dbuser = UserInDB(**user) response = await collection.insert_one(dbuser.dict()) return {"id_inserted": str(response.inserted_id)}