Hashing passwords via FastAPI
pip install passlib[bcrypt]
# If the above does not work use:
pip install passlib
pip install bcryptfrom passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")@app.post("/users", status_code=status.HTTP_201_CREATED, response_model=schemas.UserOut)
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db), ):
# Hash the Password - user.password
hashed_password = pwd_context.hash(user.password)
user.password = hashed_password
new_user = models.User(**user.dict())
db.add(new_user)
db.commit()
db.refresh(new_user)
return new_userA better approach would be to create an additional file where we would store useful code
Last updated