-
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.
Merge pull request #1 from JohnGrubba/dev
SignUp E-Mail
- Loading branch information
Showing
22 changed files
with
117 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
**.venv | ||
**.env | ||
**config.json | ||
**config.json | ||
**.pyc |
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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,33 @@ | ||
# Default E-Mail Templates | ||
|
||
EZAuth uses a set of default E-mail templates to send out e-mails to users. These templates are stored in the `config/email` folder. You can customize these templates as per your requirements. | ||
|
||
!!! Info "E-Mail Subject" | ||
EZAuth will automatically use the HTML `<title></title>` tag to specify the subject of the e-mail. | ||
|
||
## Required E-Mail Templates | ||
|
||
??? Warning "Required Templates" | ||
Only edit those templates and don't delete them. If you delete them, the service will not work as expected. | ||
|
||
### 1. **Email Verification** | ||
- Can be enabled in the `config.json` file. | ||
- File Name: `ConfirmEmail.html` | ||
|
||
#### Additional Placeholders | ||
- `{{code}}`: The confirmation code to confirm the email address. | ||
- `{{time}}`: Time remaining before the confirmation code expires in minutes. (e.g. 5) | ||
|
||
!!! Info "Default Placeholders" | ||
Every E-Mail template which is directed at a registered user will be able to use any of the properties of the user in the database. This includes the `username`, `email`, `id`, and any other property you might have added to the user object. | ||
|
||
### 2. **Welcome Email** | ||
- Can be enabled in the `config.json` file. | ||
- Will be sent out after the user has successfully verified their email address. | ||
- File Name: `WelcomeMail.html` | ||
|
||
# Custom E-Mail Templates | ||
|
||
!!! Info "Work in Progress" | ||
This feature is still in development and might not work as expected. | ||
You can also create custom E-mail templates and use them in the service. To use a custom E-mail template you need to specify the path to the template in the `config.json` file. |
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
@@ -1,8 +1,17 @@ | ||
from fastapi import APIRouter | ||
from api.model import UserSignupRequest | ||
from tools import db_collection | ||
from tools import users_collection | ||
from tools.mail import send_email | ||
from expiringdict import ExpiringDict | ||
from tools import SignupConfig | ||
from expiring_dict import ExpiringDict | ||
import random | ||
|
||
# Create an ExpiringDict object to store temporary accounts (not email verified yet) | ||
temp_accounts = ExpiringDict(ttl=SignupConfig.conf_code_expiry * 60, interval=10) | ||
|
||
# Generate and shuffle 10000 unique IDs for confirmation email | ||
all_ids = [f"{i:04d}" for i in range(10000)] | ||
random.shuffle(all_ids) | ||
|
||
router = APIRouter( | ||
prefix="/signup", | ||
|
@@ -14,12 +23,25 @@ | |
|
||
@router.post("/", status_code=204) | ||
async def signup(signup_form: UserSignupRequest): | ||
print(signup_form.model_dump()) | ||
db_collection.insert_one(signup_form.model_dump()) | ||
pass | ||
|
||
if SignupConfig.enable_conf_email: | ||
# If all numbers have been used, raise an exception | ||
if not all_ids: | ||
raise Exception( | ||
"All unique IDs have been used. More than 10000 signups in a short time." | ||
) | ||
# Get a unique ID for confirmation email | ||
unique_id = all_ids.pop() | ||
# Save the Account into the expiring dict (delete if user refuses to confirm email in time) | ||
temp_accounts[unique_id] = signup_form | ||
|
||
@router.get("/new") | ||
async def new(): | ||
send_email("WelcomeEmail", "[email protected]", username="Jonas") | ||
pass | ||
# Generate and send confirmation email | ||
send_email( | ||
"ConfirmEmail", | ||
signup_form.email, | ||
code=unique_id, | ||
time=SignupConfig.conf_code_expiry, | ||
username=signup_form.username, | ||
) | ||
else: | ||
# Save the Account into the database | ||
users_collection.insert_one(signup_form.model_dump()) |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
fastapi==0.111.0 | ||
pymongo==4.7.3 | ||
expiringdict==1.2.2 | ||
expiring-dict==1.1.0 |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from .db import col as db_collection | ||
from .conf import config | ||
from .db import users_collection | ||
from .conf import SignupConfig, EmailConfig |
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
@@ -1,3 +1,17 @@ | ||
import json | ||
|
||
config = json.load(open("/src/app/config/config.json", "rb")) | ||
|
||
|
||
class SignupConfig: | ||
enable_conf_email: bool = config["signup"]["enable_conf_email"] | ||
conf_code_expiry: int = config["signup"]["conf_code_expiry"] | ||
enable_welcome_email: bool = config["signup"]["enable_welcome_email"] | ||
|
||
|
||
class EmailConfig: | ||
login_usr: str = config["email"]["login_usr"] | ||
login_pwd: str = config["email"]["login_pwd"] | ||
sender_email: str = config["email"]["sender_email"] | ||
smtp_host: str = config["email"]["smtp_host"] | ||
smtp_port: int = config["email"]["smtp_port"] |
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