Back to articles
User Model & Auth Basics: password Hashing with Bcrypt in FastAPI

User Model & Auth Basics: password Hashing with Bcrypt in FastAPI

via Dev.to PythonFiyinfoluwa Ojo

Never Store Passwords in Plain Text This is one of the most important rules in backend development. If your database gets breached and passwords are plain text, every user's account everywhere is compromised. Hashing solves this; a hashed password can't be reversed. The User Model class User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True, index=True) email = Column(String, unique=True, nullable=False) password = Column(String, nullable=False) created_at = Column(DateTime, default=datetime.utcnow) The Signup Endpoint @app.post("/auth/signup", response_model=UserResponseDTO, status_code=201) def signup(data: SignupDTO): # Check if email already exists existing = db.query(User).filter(User.email == data.email).first() if existing: raise HTTPException(status_code=400, detail="Email already registered") # Hash the password hashed_password = bcrypt.hashpw( data.password.encode("utf-8"), bcrypt.gensalt() ) new_user = User(email=data.email, password=hashed_password.decode

Continue reading on Dev.to Python

Opens in a new tab

Read Full Article
3 views

Related Articles