-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* forgot password feature * improved config * nit * nit
- Loading branch information
Showing
21 changed files
with
415 additions
and
123 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,80 @@ | ||
import smtplib | ||
from email.mime.multipart import MIMEMultipart | ||
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 | ||
|
||
|
||
def send_email( | ||
user_email: str, | ||
subject: str, | ||
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 | ||
if mail_from: | ||
msg["From"] = mail_from | ||
|
||
msg.attach(MIMEText(body)) | ||
|
||
try: | ||
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as s: | ||
s.starttls() | ||
s.login(SMTP_USER, SMTP_PASS) | ||
s.send_message(msg) | ||
except Exception as e: | ||
raise e | ||
|
||
|
||
def send_user_email_invite(user_email: str, current_user: User) -> None: | ||
subject = "Invitation to Join Onyx Workspace" | ||
body = dedent( | ||
f"""\ | ||
Hello, | ||
You have been invited to join a workspace on Onyx. | ||
To join the workspace, please visit the following link: | ||
{WEB_DOMAIN}/auth/login | ||
Best regards, | ||
The Onyx Team | ||
""" | ||
) | ||
send_email(user_email, subject, body, current_user.email) | ||
|
||
|
||
def send_forgot_password_email( | ||
user_email: str, | ||
token: str, | ||
mail_from: str = EMAIL_FROM, | ||
) -> None: | ||
subject = "Onyx Forgot Password" | ||
link = f"{WEB_DOMAIN}/auth/reset-password?token={token}" | ||
body = f"Click the following link to reset your password: {link}" | ||
send_email(user_email, subject, body, mail_from) | ||
|
||
|
||
def send_user_verification_email( | ||
user_email: str, | ||
token: str, | ||
mail_from: str = EMAIL_FROM, | ||
) -> None: | ||
subject = "Onyx Email Verification" | ||
link = f"{WEB_DOMAIN}/auth/verify-email?token={token}" | ||
body = f"Click the following link to verify your email address: {link}" | ||
send_email(user_email, subject, body, mail_from) |
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 |
---|---|---|
|
@@ -92,6 +92,7 @@ | |
SMTP_PORT = int(os.environ.get("SMTP_PORT") or "587") | ||
SMTP_USER = os.environ.get("SMTP_USER", "[email protected]") | ||
SMTP_PASS = os.environ.get("SMTP_PASS", "your-gmail-password") | ||
EMAIL_CONFIGURED = all([SMTP_SERVER, SMTP_USER, SMTP_PASS]) | ||
EMAIL_FROM = os.environ.get("EMAIL_FROM") or SMTP_USER | ||
|
||
# If set, Onyx will listen to the `expires_at` returned by the identity | ||
|
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
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
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
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
Oops, something went wrong.