forked from ujjawalpoudel/chatbot-with-flask-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
login check with additional save password with hashing
- Loading branch information
1 parent
95f9eaa
commit b4b9d5f
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |