JobSathi_Backend / utils.py
AhmedShahan's picture
Implement user registration and update login functionality with password verification
d36ec6b
Raw History Blame Contribute Delete
1.02 kB
import os
import psycopg2
from argon2 import PasswordHasher
from dotenv import load_dotenv
# Load variables from .env file
load_dotenv()
DB_HOST = os.getenv("DB_HOST")
DB_PORT = os.getenv("DB_PORT")
DB_NAME = os.getenv("DB_NAME")
DB_USER = os.getenv("DB_USER")
DB_PASSWORD = os.getenv("DB_PASSWORD")
_hasher = PasswordHasher()
def get_connection():
"""Open a new connection to the Supabase Postgres database."""
return psycopg2.connect(
host=DB_HOST,
port=DB_PORT,
dbname=DB_NAME,
user=DB_USER,
password=DB_PASSWORD,
)
def hash_password(password: str) -> str:
"""Return an Argon2 hash string for the given plaintext password."""
return _hasher.hash(password)
def verify_password(password: str, hashed: str) -> bool:
"""Verify a plaintext password against an Argon2 hash string.
Returns False on mismatch or invalid hash; never raises.
"""
try:
return _hasher.verify(hashed, password)
except Exception:
return False