Skip to content

Commit

Permalink
login check with additional save password with hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjawalpoudel committed Apr 10, 2023
1 parent 95f9eaa commit b4b9d5f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/routes/chatbotCRUD.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from app.validators.models.chatbotUserValidators import UserModel, UpdateUserModel
from service.errorHandler import error_handler
from service.pydanticDecorator import pydantic_validation
from utils.passwordHash import hash_password
from app.models.chatbotDbModel import User
from service.response import response

Expand All @@ -24,6 +25,7 @@
def create_user_main():
# * Get Data from Frontend
data = json.loads(request.data)
data["password"] = hash_password(data["password"])

# * Save Data in Mongodb
user = User(**data).save()
Expand Down
41 changes: 41 additions & 0 deletions app/routes/login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# * Import Python Module
import json
from flask import Blueprint, request
from mongoengine import DoesNotExist


# * Import Patient Defined Functions
from app.validators.models.loginValidators import LoginModel
from service.pydanticDecorator import pydantic_validation
from app.models.chatbotDbModel import User
from utils.passwordHash import check_password, hash_password
from service.response import response


# * Define Blueprint for API Routes
login_module = Blueprint("login_module", __name__)


# * Define API Route for Login
@login_module.route("/login", methods=["POST"], endpoint="login")
@pydantic_validation(LoginModel)
def login():
try:
# * Get Data from Frontend
data = json.loads(request.data)

email = data["email"]
password = data["password"]

# * Get Data from Mongodb
user = User.objects.get(email=email)
print(hash_password(password))
print(user.password)

# Check if a password matches the hashed password
if check_password(password, user.password):
return response(302, {"msg": "Successfully Login"})
else:
return response(401, {"msg": "Invalid password"})
except DoesNotExist:
return response(401, {"msg": "Invalid Email Address"})
17 changes: 17 additions & 0 deletions app/validators/models/loginValidators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pydantic

from utils.validationCheck import email_check


class LoginModel(pydantic.BaseModel, extra=pydantic.Extra.forbid):
email: str
password: str

@pydantic.validator("email")
@classmethod
def email_valid_check(cls, email_address) -> None:
if email_check(email_address):
return email_address
else:
message = "Given email ({0}) is not valid.".format(email_address)
raise ValueError(message)
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# * Import User Defined Functions
from app.routes.chatbotCRUD import chatbot_user_module
from app.routes.patientCRUD import patient_module
from app.routes.login import login_module
from app.routes.chatbotResponseCRUD import chatbot_response_module
from config import host_uri

Expand All @@ -21,6 +22,7 @@ def hello_world():

app.register_blueprint(chatbot_user_module, url_prefix="/users")
app.register_blueprint(patient_module, url_prefix="/patient")
app.register_blueprint(login_module)
app.register_blueprint(chatbot_response_module, url_prefix="/chatbot-response")

# Define the MongoDB connection
Expand Down
16 changes: 16 additions & 0 deletions utils/passwordHash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import bcrypt


# Hash a password for storing in a database
def hash_password(password):
# Generate a random salt
salt = bcrypt.gensalt()
# Hash the password with the salt
hashed_password = bcrypt.hashpw(password.encode("utf-8"), salt)
return hashed_password


# Check if a password matches a hash stored in the database
def check_password(password, hashed_password):
# Check if the password matches the hash
return bcrypt.checkpw(password.encode("utf-8"), hashed_password.encode("utf-8"))

0 comments on commit b4b9d5f

Please sign in to comment.