Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store the users hashed password using bcrypt instead of the plaintext… #255

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion twitter/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
reflex>=0.4.0
bcrypt>=4.2.0
reflex>=0.5.10
7 changes: 3 additions & 4 deletions twitter/twitter/db_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from sqlmodel import Field

import reflex as rx
from sqlmodel import Field


class Follows(rx.Model, table=True):
Expand All @@ -17,7 +16,7 @@ class User(rx.Model, table=True):
"""A table of Users."""

username: str
password: str
password: bytes


class Tweet(rx.Model, table=True):
Expand All @@ -26,4 +25,4 @@ class Tweet(rx.Model, table=True):
content: str
created_at: str

author: str
author: str
9 changes: 7 additions & 2 deletions twitter/twitter/state/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""The authentication state."""

import bcrypt
import reflex as rx
from sqlmodel import select

Expand All @@ -19,7 +21,10 @@ def signup(self):
return rx.window_alert("Passwords do not match.")
if session.exec(select(User).where(User.username == self.username)).first():
return rx.window_alert("Username already exists.")
self.user = User(username=self.username, password=self.password)
hashed_password = bcrypt.hashpw(
self.password.encode("utf-8"), bcrypt.gensalt()
)
self.user = User(username=self.username, password=hashed_password)
session.add(self.user)
session.expire_on_commit = False
session.commit()
Expand All @@ -31,7 +36,7 @@ def login(self):
user = session.exec(
select(User).where(User.username == self.username)
).first()
if user and user.password == self.password:
if user and bcrypt.checkpw(self.password.encode("utf-8"), user.password):
self.user = user
return rx.redirect("/")
else:
Expand Down