Skip to content

Commit

Permalink
ADDED: Email verification while SignUp
Browse files Browse the repository at this point in the history
  • Loading branch information
AquibPy committed May 14, 2024
1 parent cd3a92c commit 76336d6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
45 changes: 41 additions & 4 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from jose import jwt, JWTError
import settings
from models import UserCreate, ResponseText
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from uuid import uuid4


os.environ["LANGCHAIN_TRACING_V2"]="true"
Expand Down Expand Up @@ -65,11 +68,45 @@ async def signup(user: UserCreate):
if existing_user:
raise HTTPException(status_code=400, detail="Email already registered")

# Insert new user to database
user_dict = user.model_dump()
users_collection.insert_one(user_dict)
# Insert new user to database with email_verified set to False
verification_token = str(uuid4())
new_user = {
"email": user.email,
"password": user.password,
"email_verified": False,
"verification_token": verification_token
}
users_collection.insert_one(new_user)

# Send verification email
message = Mail(
from_email='[email protected]',
to_emails=user.email,
subject='Verify your email',
html_content=f'Please verify your email using this token: {verification_token}'
)

try:
sendgrid_api = os.getenv("SENDGRID_API_KEY")
sg = SendGridAPIClient(sendgrid_api)
response = sg.send(message)
print(response.status_code)
except Exception as e:
print(e.message)

return {"message": "User created successfully. Please check your email to verify your account."}

@app.post("/verify-email")
async def verify_email(token: str):
# Find user with the provided verification token
user = users_collection.find_one({"verification_token": token})
if not user:
raise HTTPException(status_code=400, detail="Invalid token")

# Mark the user's email as verified
users_collection.update_one({"_id": user["_id"]}, {"$set": {"email_verified": True}})

return {"message": "User created successfully"}
return {"message": "Email verified successfully"}

# Signin route
@app.post("/token")
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ langchain-groq
jinja2
tiktoken
redis
python-jose
python-jose
sendgrid
2 changes: 1 addition & 1 deletion settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GEMINI_PRO = "gemini-pro"
GEMINI_PRO_1_5 = "gemini-1.5-pro-latest"
GOOGLE_EMBEDDING = "models/embedding-001"
FAQ_FILE = 'data\\faqs.csv'
FAQ_FILE = 'data/faqs.csv'
EMPLOYEE_DB = "data/employees.db"
INSTRUCTOR_EMBEDDING = "sentence-transformers/all-MiniLM-l6-v2"
VECTORDB_PATH = "faiss_index"
Expand Down

0 comments on commit 76336d6

Please sign in to comment.