Skip to content

Commit

Permalink
minor nit
Browse files Browse the repository at this point in the history
  • Loading branch information
pablonyx committed Dec 18, 2024
1 parent 5c5f2fb commit 071d03d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions backend/onyx/auth/email_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
from email.mime.text import MIMEText
from textwrap import dedent

from onyx.configs.app_configs import EMAIL_CONFIGURED
from onyx.configs.app_configs import EMAIL_FROM
from onyx.configs.app_configs import SMTP_PASS
from onyx.configs.app_configs import SMTP_PORT
from onyx.configs.app_configs import SMTP_SERVER
from onyx.configs.app_configs import SMTP_USER
from onyx.configs.app_configs import WEB_DOMAIN
from onyx.db.models import User
from onyx.setup import setup_logger

logger = setup_logger(__name__)


def send_email(
Expand All @@ -18,6 +22,9 @@ def send_email(
body: str,
mail_from: str = EMAIL_FROM,
) -> None:
if not EMAIL_CONFIGURED:
raise ValueError("Email is not configured.")

msg = MIMEMultipart()
msg["Subject"] = subject
msg["To"] = user_email
Expand Down
8 changes: 7 additions & 1 deletion backend/onyx/auth/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
from onyx.auth.schemas import UserUpdate
from onyx.configs.app_configs import AUTH_TYPE
from onyx.configs.app_configs import DISABLE_AUTH
from onyx.configs.app_configs import EMAIL_CONFIGURED
from onyx.configs.app_configs import REQUIRE_EMAIL_VERIFICATION
from onyx.configs.app_configs import SESSION_EXPIRE_TIME_SECONDS
from onyx.configs.app_configs import TRACK_EXTERNAL_IDP_EXPIRY
Expand Down Expand Up @@ -470,7 +471,12 @@ async def on_after_register(
async def on_after_forgot_password(
self, user: User, token: str, request: Optional[Request] = None
) -> None:
logger.notice(f"User {user.id} has forgot their password. Reset token: {token}")
if not EMAIL_CONFIGURED:
logger.error(
"User forgot their password but email is not configured.",
extra={"user_email": user.email},
)
return
send_forgot_password_email(user.email, token)

async def on_after_request_verify(
Expand Down
3 changes: 3 additions & 0 deletions backend/onyx/configs/app_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
SMTP_PORT = int(os.environ.get("SMTP_PORT") or "587")
SMTP_USER = os.environ.get("SMTP_USER", "your-email@gmail.com")
SMTP_PASS = os.environ.get("SMTP_PASS", "your-gmail-password")
EMAIL_CONFIGURED = (
SMTP_SERVER is not None and SMTP_USER is not None and SMTP_PASS is not None
)
EMAIL_FROM = os.environ.get("EMAIL_FROM") or SMTP_USER

# If set, Onyx will listen to the `expires_at` returned by the identity
Expand Down

0 comments on commit 071d03d

Please sign in to comment.