From 512483e6d24fe34a7ae0d7604ca0a8e879a447ad Mon Sep 17 00:00:00 2001 From: DMYTRO YURCHYSHYN Date: Tue, 4 Feb 2025 13:31:53 +0200 Subject: [PATCH 1/2] Fix user activation link handling via MailHog --- src/main.py | 9 ++ src/notifications/emails.py | 8 +- src/notifications/interfaces.py | 4 +- .../templates/activation_request.html | 105 ++++++++++++------ src/routes/accounts.py | 8 +- 5 files changed, 97 insertions(+), 37 deletions(-) diff --git a/src/main.py b/src/main.py index af2efa6..f7698f8 100644 --- a/src/main.py +++ b/src/main.py @@ -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 @@ -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"] ) diff --git a/src/notifications/emails.py b/src/notifications/emails.py index 75c0bdd..83b517b 100644 --- a/src/notifications/emails.py +++ b/src/notifications/emails.py @@ -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) diff --git a/src/notifications/interfaces.py b/src/notifications/interfaces.py index 0503c7a..03adc6a 100644 --- a/src/notifications/interfaces.py +++ b/src/notifications/interfaces.py @@ -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 diff --git a/src/notifications/templates/activation_request.html b/src/notifications/templates/activation_request.html index fb4c0bd..8257d53 100644 --- a/src/notifications/templates/activation_request.html +++ b/src/notifications/templates/activation_request.html @@ -1,35 +1,78 @@ - + + - Registration + + + Account Activation + -
-

- Welcome to Our Platform -

-

- A registration request was made using your email: - {{ email }}. -

-

- To complete your registration, please use the activation token below: -

-

- - Activate Your Account - -

-

- If this wasn't you, please ignore this email. -

-

- Regards, -

-

- The FastAPI team -

-
+
+

+ Welcome to Our Platform +

+

+ A registration request was made using your email: + {{ email }}. +

+

+ To complete your registration, please click the button below: +

+
+ +
+

+ If this wasn't you, please ignore this email. +

+

+ Regards, +

+

+ The FastAPI Team +

+
- \ No newline at end of file + diff --git a/src/routes/accounts.py b/src/routes/accounts.py index aba4ed0..297dde6 100644 --- a/src/routes/accounts.py +++ b/src/routes/accounts.py @@ -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) From d4b088859c575292157ebdb635f133e79e8dfc35 Mon Sep 17 00:00:00 2001 From: DMYTRO YURCHYSHYN Date: Tue, 4 Feb 2025 13:40:14 +0200 Subject: [PATCH 2/2] formatted activation_request.html file using Prettier --- .../templates/activation_request.html | 185 +++++++++++------- 1 file changed, 116 insertions(+), 69 deletions(-) diff --git a/src/notifications/templates/activation_request.html b/src/notifications/templates/activation_request.html index 8257d53..96ebb47 100644 --- a/src/notifications/templates/activation_request.html +++ b/src/notifications/templates/activation_request.html @@ -1,78 +1,125 @@ - + - - - - Account Activation - - - -
-

- Welcome to Our Platform -

-

- A registration request was made using your email: - {{ email }}. -

-

- To complete your registration, please click the button below: -

-
- -
-

- If this wasn't you, please ignore this email. -

-

- Regards, -

-

- The FastAPI Team -

-
- + + + +
+

+ Welcome to Our Platform +

+

+ A registration request was made using your email: + {{ email }}. +

+

+ To complete your registration, please click the button below: +

+
+ +
+

+ If this wasn't you, please ignore this email. +

+

+ Regards, +

+

+ The FastAPI Team +

+
+