Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fastapi import FastAPI
from fastapi_pagination import add_pagination
from fastapi.middleware.cors import CORSMiddleware

from routes import (
accounts_router, movies_router, payments_router, profiles_router, orders_router
Expand All @@ -12,6 +13,14 @@

api_version_prefix = "/api/v1"

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

app.include_router(
accounts_router, prefix=f"{api_version_prefix}/accounts", tags=["accounts"]
)
Expand Down
8 changes: 6 additions & 2 deletions src/notifications/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ def _send_email(self, email: str, subject: str, html_content: str) -> None:
logging.error(f"Failed to send email to {email}: {error}")
raise BaseEmailError(f"Failed to send email to {email}: {error}")

def send_activation_email(self, email: str, activation_link: str) -> None:
def send_activation_email(
self, email: str, token: str, activation_link: str
) -> None:
template = self._env.get_template(self._activation_email_template_name)
html_content = template.render(email=email, activation_link=activation_link)
html_content = template.render(
email=email, token=token, activation_link=activation_link
)

subject = "Account Activation"
self._send_email(email, subject, html_content)
Expand Down
4 changes: 3 additions & 1 deletion src/notifications/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
class EmailSenderInterface(ABC):

@abstractmethod
def send_activation_email(self, email: str, activation_link: str) -> None:
def send_activation_email(
self, email: str, token: str, activation_link: str
) -> None:
pass

@abstractmethod
Expand Down
160 changes: 125 additions & 35 deletions src/notifications/templates/activation_request.html
Original file line number Diff line number Diff line change
@@ -1,35 +1,125 @@
<html lang="">
<head>
<title>Registration</title>
</head>
<body style="margin: 0; padding: 0; background-color: #f4f4f4; font-family: Arial, sans-serif;">
<div style="border: 1px solid #e0e0e0; border-radius: 8px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
padding: 20px; max-width: 480px; margin: 40px auto; background-color: #ffffff; color: #333333;">
<h2 style="color: #4CAF50; text-align: center; margin: 0 0 20px; font-size: 24px; font-weight: bold;">
Welcome to Our Platform
</h2>
<p style="margin: 10px 0; line-height: 1.6; font-size: 16px;">
A registration request was made using your email:
<strong id="email" style="color: #4CAF50;">{{ email }}</strong>.
</p>
<p style="margin: 10px 0; line-height: 1.6; font-size: 16px;">
To complete your registration, please use the activation token below:
</p>
<p style="margin: 20px 0; text-align: center;">
<a id="link" href="{{ activation_link }}" style="display: inline-block; background-color: #4CAF50; color: #ffffff;
text-decoration: none; font-size: 16px; padding: 10px 20px; border-radius: 4px;">
Activate Your Account
</a>
</p>
<p style="margin: 10px 0; line-height: 1.6; font-size: 16px; color: #666666;">
If this wasn't you, please ignore this email.
</p>
<p style="margin: 20px 0 10px; line-height: 1.6; font-size: 16px;">
Regards,
</p>
<p style="margin: 0; font-style: italic; font-size: 16px; color: #4CAF50;">
The FastAPI team
</p>
</div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Account Activation</title>
<script>
async function activateAccount(event) {
event.preventDefault();

const email = "{{ email }}";
const token = "{{ token }}";
const activationLink = "{{ activation_link }}";

const data = {
email: email,
token: token,
};

const activateButton = document.getElementById("activateButton");
activateButton.disabled = true;
activateButton.textContent = "Activating...";

try {
const response = await fetch(activationLink, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});

if (response.ok) {
alert("Account activated successfully!");
} else {
alert(
"Your account is already activated or the activation link is invalid.",
);
}
} catch (error) {
console.error("Error:", error);
alert("An error occurred. Please try again.");
} finally {
activateButton.disabled = false;
activateButton.textContent = "Activate Your Account";
}
}
</script>
</head>
<body
style="
margin: 0;
padding: 0;
background-color: #f4f4f4;
font-family: Arial, sans-serif;
"
>
<div
style="
border: 1px solid #e0e0e0;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
padding: 20px;
max-width: 480px;
margin: 40px auto;
background-color: #ffffff;
color: #333333;
"
>
<h2
style="
color: #4caf50;
text-align: center;
margin: 0 0 20px;
font-size: 24px;
font-weight: bold;
"
>
Welcome to Our Platform
</h2>
<p style="margin: 10px 0; line-height: 1.6; font-size: 16px">
A registration request was made using your email:
<strong id="email" style="color: #4caf50">{{ email }}</strong>.
</p>
<p style="margin: 10px 0; line-height: 1.6; font-size: 16px">
To complete your registration, please click the button below:
</p>
<form onsubmit="activateAccount(event)" style="text-align: center">
<button
type="submit"
id="activateButton"
style="
display: inline-block;
background-color: #4caf50;
color: #ffffff;
text-decoration: none;
font-size: 16px;
padding: 10px 20px;
border-radius: 4px;
border: none;
cursor: pointer;
"
>
Activate Your Account
</button>
</form>
<p
style="
margin: 10px 0;
line-height: 1.6;
font-size: 16px;
color: #666666;
"
>
If this wasn't you, please ignore this email.
</p>
<p style="margin: 20px 0 10px; line-height: 1.6; font-size: 16px">
Regards,
</p>
<p style="margin: 0; font-style: italic; font-size: 16px; color: #4caf50">
The FastAPI Team
</p>
</div>
</body>
</html>
8 changes: 5 additions & 3 deletions src/routes/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ def register_user(
detail="An error occurred during user creation.",
)
else:
activation_link = "http://127.0.0.1/accounts/activate/"

activation_link = "http://127.0.0.1:8000/api/v1/accounts/activate/"
background_tasks.add_task(
email_sender.send_activation_email, new_user.email, activation_link
email_sender.send_activation_email,
new_user.email,
activation_token.token,
activation_link
)

return UserRegistrationResponseSchema.model_validate(new_user)
Expand Down