From 33d5f9158a54b12b98473b04027fe3fc81c5ba09 Mon Sep 17 00:00:00 2001 From: Edem Date: Fri, 15 Jul 2022 07:39:23 +0000 Subject: [PATCH 01/14] updated --- readMe.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 readMe.md diff --git a/readMe.md b/readMe.md new file mode 100644 index 0000000..1089b05 --- /dev/null +++ b/readMe.md @@ -0,0 +1,5 @@ +# RESTful API with Python, FastAPI, Pydantic, and MongoDB + +### 1. API with Python, FastAPI, and MongoDB: JWT Authentication + +[API with Python, FastAPI, and MongoDB: JWT Authentication](https://codevoweb.com/api-with-python-fastapi-and-mongodb-jwt-authentication) From 131427a4dd6d4ab160a072b406d27f25e2fbec4f Mon Sep 17 00:00:00 2001 From: Edem Date: Fri, 15 Jul 2022 11:47:16 +0000 Subject: [PATCH 02/14] updated --- app/database.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/database.py b/app/database.py index ed87d09..fad8ae7 100644 --- a/app/database.py +++ b/app/database.py @@ -2,8 +2,14 @@ import pymongo from app.config import settings -client = mongo_client.MongoClient(settings.DATABASE_URL) -print('Connected to MongoDB...') +client = mongo_client.MongoClient( + settings.DATABASE_URL, serverSelectionTimeoutMS=5000) + +try: + conn = client.server_info() + print(f'Connected to MongoDB {conn.get("version")}') +except Exception: + print("Unable to connect to the MongoDB server.") db = client[settings.MONGO_INITDB_DATABASE] User = db.users From 044a66c324083391ef957c159cf30b50008fb7a4 Mon Sep 17 00:00:00 2001 From: Edem Date: Mon, 18 Jul 2022 11:25:20 +0000 Subject: [PATCH 03/14] updated --- readMe.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/readMe.md b/readMe.md index 1089b05..fb75b6a 100644 --- a/readMe.md +++ b/readMe.md @@ -3,3 +3,7 @@ ### 1. API with Python, FastAPI, and MongoDB: JWT Authentication [API with Python, FastAPI, and MongoDB: JWT Authentication](https://codevoweb.com/api-with-python-fastapi-and-mongodb-jwt-authentication) + +### 2. Build API with Python & FastAPI: SignUp User and Verify Email + +[Build API with Python & FastAPI: SignUp User and Verify Email](https://codevoweb.com/api-with-python-fastapi-signup-user-and-verify-email) From d806c409a49aa772ff16a25eac32db69125a86f7 Mon Sep 17 00:00:00 2001 From: Edem Date: Wed, 20 Jul 2022 06:16:14 +0000 Subject: [PATCH 04/14] updated --- app/oauth2.py | 2 +- app/routers/auth.py | 2 +- app/routers/user.py | 2 +- app/{serializers.py => serializers/userSerializers.py} | 9 +++++++++ 4 files changed, 12 insertions(+), 3 deletions(-) rename app/{serializers.py => serializers/userSerializers.py} (80%) diff --git a/app/oauth2.py b/app/oauth2.py index 517960f..11146cd 100644 --- a/app/oauth2.py +++ b/app/oauth2.py @@ -5,7 +5,7 @@ from pydantic import BaseModel from bson.objectid import ObjectId -from app.serializers import userEntity +from app.serializers.userSerializers import userEntity from .database import User from .config import settings diff --git a/app/routers/auth.py b/app/routers/auth.py index a4472fb..a51dfc7 100644 --- a/app/routers/auth.py +++ b/app/routers/auth.py @@ -8,7 +8,7 @@ from app import oauth2 from app.database import User from app.email import Email -from app.serializers import userEntity +from app.serializers.userSerializers import userEntity from .. import schemas, utils from app.oauth2 import AuthJWT from ..config import settings diff --git a/app/routers/user.py b/app/routers/user.py index ae1c722..46a5917 100644 --- a/app/routers/user.py +++ b/app/routers/user.py @@ -1,6 +1,6 @@ from fastapi import APIRouter, Depends from bson.objectid import ObjectId -from app.serializers import userResponseEntity +from app.serializers.userSerializers import userResponseEntity from app.database import User from .. import schemas, oauth2 diff --git a/app/serializers.py b/app/serializers/userSerializers.py similarity index 80% rename from app/serializers.py rename to app/serializers/userSerializers.py index 9a82e3f..fc25f84 100644 --- a/app/serializers.py +++ b/app/serializers/userSerializers.py @@ -24,5 +24,14 @@ def userResponseEntity(user) -> dict: } +def embeddedUserResponse(user) -> dict: + return { + "id": str(user["_id"]), + "name": user["name"], + "email": user["email"], + "photo": user["photo"] + } + + def userListEntity(users) -> list: return [userEntity(user) for user in users] From 4cd62c1885721f04f8b449a1de664a39c14ceb3f Mon Sep 17 00:00:00 2001 From: Edem Date: Wed, 20 Jul 2022 10:44:07 +0000 Subject: [PATCH 05/14] updated --- readMe.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/readMe.md b/readMe.md index fb75b6a..7759a39 100644 --- a/readMe.md +++ b/readMe.md @@ -7,3 +7,7 @@ ### 2. Build API with Python & FastAPI: SignUp User and Verify Email [Build API with Python & FastAPI: SignUp User and Verify Email](https://codevoweb.com/api-with-python-fastapi-signup-user-and-verify-email) + +### 3. CRUD RESTful API Server with Python, FastAPI, and MongoDB + +[CRUD RESTful API Server with Python, FastAPI, and MongoDB](https://codevoweb.com/crud-restful-api-server-with-python-fastapi-and-mongodb) From 09eeee41d6a21ef7c2258abc9991c44765f098fe Mon Sep 17 00:00:00 2001 From: Edem Date: Sun, 28 Aug 2022 12:36:54 +0000 Subject: [PATCH 06/14] updated --- requirements.txt | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2204aa0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,56 @@ +aioredis==2.0.1 +aiosmtplib==1.1.6 +anyio==3.6.1 +asgiref==3.5.2 +async-timeout==4.0.2 +autopep8==1.6.0 +bcrypt==3.2.2 +blinker==1.4 +certifi==2022.6.15 +cffi==1.15.1 +charset-normalizer==2.1.0 +click==8.1.3 +colorama==0.4.5 +cryptography==3.4.8 +Deprecated==1.2.13 +dnspython==2.2.1 +email-validator==1.2.1 +fakeredis==1.8.1 +fastapi==0.78.0 +fastapi-jwt-auth==0.5.0 +fastapi-mail==1.0.9 +h11==0.12.0 +httpcore==0.15.0 +httptools==0.4.0 +httpx==0.23.0 +idna==3.3 +itsdangerous==2.1.2 +Jinja2==3.1.2 +MarkupSafe==2.1.1 +orjson==3.7.7 +packaging==21.3 +passlib==1.7.4 +pycodestyle==2.8.0 +pycparser==2.21 +pydantic==1.9.1 +PyJWT==1.7.1 +pymongo==4.1.1 +pyparsing==3.0.9 +python-dotenv==0.20.0 +python-multipart==0.0.5 +PyYAML==6.0 +redis==4.3.4 +requests==2.28.1 +rfc3986==1.5.0 +six==1.16.0 +sniffio==1.2.0 +sortedcontainers==2.4.0 +starlette==0.19.1 +toml==0.10.2 +typing_extensions==4.3.0 +ujson==5.4.0 +urllib3==1.26.10 +uvicorn==0.17.6 +watchgod==0.8.2 +websockets==10.3 +wrapt==1.14.1 From 7b28275ddc9413aff11360d2ae20cf98ef8877f7 Mon Sep 17 00:00:00 2001 From: Edem Date: Sun, 28 Aug 2022 12:52:42 +0000 Subject: [PATCH 07/14] updated --- example.env | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 example.env diff --git a/example.env b/example.env new file mode 100644 index 0000000..e4d3db5 --- /dev/null +++ b/example.env @@ -0,0 +1,20 @@ +MONGO_INITDB_ROOT_USERNAME=admin +MONGO_INITDB_ROOT_PASSWORD=password123 +MONGO_INITDB_DATABASE=fastapi + +DATABASE_URL=mongodb://admin:password123@localhost:6000/fastapi?authSource=admin + +ACCESS_TOKEN_EXPIRES_IN=15 +REFRESH_TOKEN_EXPIRES_IN=60 +JWT_ALGORITHM=RS256 + +CLIENT_ORIGIN=http://localhost:3000 + +EMAIL_HOST=smtp.mailtrap.io +EMAIL_PORT=587 +EMAIL_USERNAME=90cf952fb44469 +EMAIL_PASSWORD=0524531956c552 +EMAIL_FROM=admin@admin.com + +JWT_PRIVATE_KEY=LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlCT2dJQkFBSkJBSSs3QnZUS0FWdHVQYzEzbEFkVk94TlVmcWxzMm1SVmlQWlJyVFpjd3l4RVhVRGpNaFZuCi9KVHRsd3h2a281T0pBQ1k3dVE0T09wODdiM3NOU3ZNd2xNQ0F3RUFBUUpBYm5LaENOQ0dOSFZGaHJPQ0RCU0IKdmZ2ckRWUzVpZXAwd2h2SGlBUEdjeWV6bjd0U2RweUZ0NEU0QTNXT3VQOXhqenNjTFZyb1pzRmVMUWlqT1JhUwp3UUloQU84MWl2b21iVGhjRkltTFZPbU16Vk52TGxWTW02WE5iS3B4bGh4TlpUTmhBaUVBbWRISlpGM3haWFE0Cm15QnNCeEhLQ3JqOTF6bVFxU0E4bHUvT1ZNTDNSak1DSVFEbDJxOUdtN0lMbS85b0EyaCtXdnZabGxZUlJPR3oKT21lV2lEclR5MUxaUVFJZ2ZGYUlaUWxMU0tkWjJvdXF4MHdwOWVEejBEWklLVzVWaSt6czdMZHRDdUVDSUVGYwo3d21VZ3pPblpzbnU1clBsTDJjZldLTGhFbWwrUVFzOCtkMFBGdXlnCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0t +JWT_PUBLIC_KEY=LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUZ3d0RRWUpLb1pJaHZjTkFRRUJCUUFEU3dBd1NBSkJBSSs3QnZUS0FWdHVQYzEzbEFkVk94TlVmcWxzMm1SVgppUFpSclRaY3d5eEVYVURqTWhWbi9KVHRsd3h2a281T0pBQ1k3dVE0T09wODdiM3NOU3ZNd2xNQ0F3RUFBUT09Ci0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQ== \ No newline at end of file From e7920afb2cd1aa54e65cfd7b6ed121897a4041bd Mon Sep 17 00:00:00 2001 From: CODEVO Date: Sun, 2 Oct 2022 15:53:34 +0000 Subject: [PATCH 08/14] Update readMe.md --- readMe.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/readMe.md b/readMe.md index 7759a39..8a971b5 100644 --- a/readMe.md +++ b/readMe.md @@ -1,4 +1,24 @@ -# RESTful API with Python, FastAPI, Pydantic, and MongoDB +# Build API with Python & FastAPI: SignUp User and Verify Email + +This article will teach you how to send HTML Emails with Python, FastAPI, PyMongo, MongoDB, Jinja2, and Docker. Also, you will learn how to use Jinja2 to generate different HTML templates. + +![Build API with Python & FastAPI: SignUp User and Verify Email](https://codevoweb.com/wp-content/uploads/2022/07/Build-API-with-Python-FastAPI-SignUp-User-and-Verify-Email.webp) + +## Topics Covered + +- Send HTML Emails with Jinja2 & FastAPI Example +- Creating the SMTP Provider Account +- Edit the Environment Variables File +- Validating the Environment Variables with Pydantic +- Creating the HTML Email Templates in FastAPI +- Creating the SMTP Email Sender +- Sending the HTML Emails in FastAPI +- Update the SignUp Controller +- Create a Handler to Validate the Verification Code + +Read the entire article here: [https://codevoweb.com/api-with-python-fastapi-signup-user-and-verify-email](https://codevoweb.com/api-with-python-fastapi-signup-user-and-verify-email) + +Articles in this series: ### 1. API with Python, FastAPI, and MongoDB: JWT Authentication From 8213ca6daa37f565d2b46e6b0a9e81f85ec00e50 Mon Sep 17 00:00:00 2001 From: Edem Ziddah Date: Tue, 20 Dec 2022 13:31:12 +0000 Subject: [PATCH 09/14] updated --- app/routers/auth.py | 5 ++-- requirements.txt | 69 ++++++++++++++++++--------------------------- 2 files changed, 31 insertions(+), 43 deletions(-) diff --git a/app/routers/auth.py b/app/routers/auth.py index a51dfc7..cf28605 100644 --- a/app/routers/auth.py +++ b/app/routers/auth.py @@ -62,10 +62,11 @@ async def create_user(payload: schemas.CreateUserSchema, request: Request): @router.post('/login') def login(payload: schemas.LoginUserSchema, response: Response, Authorize: AuthJWT = Depends()): # Check if the user exist - user = userEntity(User.find_one({'email': payload.email.lower()})) - if not user: + db_user = User.find_one({'email': payload.email.lower()}) + if not db_user: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='Incorrect Email or Password') + user = userEntity(db_user) # Check if user verified his email if not user['verified']: diff --git a/requirements.txt b/requirements.txt index 2204aa0..44f81f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,56 +1,43 @@ -aioredis==2.0.1 -aiosmtplib==1.1.6 -anyio==3.6.1 -asgiref==3.5.2 -async-timeout==4.0.2 -autopep8==1.6.0 -bcrypt==3.2.2 -blinker==1.4 -certifi==2022.6.15 +aiosmtplib==1.1.7 +anyio==3.6.2 +bcrypt==4.0.1 +blinker==1.5 +certifi==2022.12.7 cffi==1.15.1 -charset-normalizer==2.1.0 click==8.1.3 -colorama==0.4.5 +colorama==0.4.6 cryptography==3.4.8 -Deprecated==1.2.13 dnspython==2.2.1 -email-validator==1.2.1 -fakeredis==1.8.1 -fastapi==0.78.0 +ecdsa==0.18.0 +email-validator==1.3.0 +fastapi==0.88.0 fastapi-jwt-auth==0.5.0 -fastapi-mail==1.0.9 -h11==0.12.0 -httpcore==0.15.0 -httptools==0.4.0 -httpx==0.23.0 -idna==3.3 +fastapi-mail==1.1.5 +h11==0.14.0 +httpcore==0.16.2 +httptools==0.5.0 +httpx==0.23.1 +idna==3.4 itsdangerous==2.1.2 Jinja2==3.1.2 MarkupSafe==2.1.1 -orjson==3.7.7 -packaging==21.3 +orjson==3.8.3 passlib==1.7.4 -pycodestyle==2.8.0 +pyasn1==0.4.8 pycparser==2.21 -pydantic==1.9.1 +pydantic==1.10.2 PyJWT==1.7.1 -pymongo==4.1.1 -pyparsing==3.0.9 -python-dotenv==0.20.0 +pymongo==4.3.3 +python-dotenv==0.21.0 python-multipart==0.0.5 PyYAML==6.0 -redis==4.3.4 -requests==2.28.1 rfc3986==1.5.0 +rsa==4.9 six==1.16.0 -sniffio==1.2.0 -sortedcontainers==2.4.0 -starlette==0.19.1 -toml==0.10.2 -typing_extensions==4.3.0 -ujson==5.4.0 -urllib3==1.26.10 -uvicorn==0.17.6 -watchgod==0.8.2 -websockets==10.3 -wrapt==1.14.1 +sniffio==1.3.0 +starlette==0.22.0 +typing_extensions==4.4.0 +ujson==5.6.0 +uvicorn==0.20.0 +watchfiles==0.18.1 +websockets==10.4 From 9d181d9a923b968e56a92eaff5dc3a148c18d58b Mon Sep 17 00:00:00 2001 From: Edem Ziddah Date: Tue, 20 Dec 2022 19:22:41 +0000 Subject: [PATCH 10/14] updated --- app/email.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/email.py b/app/email.py index 28545d0..939c58f 100644 --- a/app/email.py +++ b/app/email.py @@ -31,8 +31,8 @@ async def sendMail(self, subject, template): MAIL_FROM=settings.EMAIL_FROM, MAIL_PORT=settings.EMAIL_PORT, MAIL_SERVER=settings.EMAIL_HOST, - MAIL_TLS=True, - MAIL_SSL=False, + MAIL_STARTTLS=False, + MAIL_SSL_TLS=False, USE_CREDENTIALS=True, VALIDATE_CERTS=True ) From 498cb512848369b9fe320f0cb3b4896cab9c7664 Mon Sep 17 00:00:00 2001 From: Edem Ziddah Date: Tue, 20 Dec 2022 19:27:54 +0000 Subject: [PATCH 11/14] updated --- requirements.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 44f81f8..85c1279 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ aiosmtplib==1.1.7 anyio==3.6.2 +autopep8==2.0.1 bcrypt==4.0.1 blinker==1.5 certifi==2022.12.7 @@ -12,7 +13,7 @@ ecdsa==0.18.0 email-validator==1.3.0 fastapi==0.88.0 fastapi-jwt-auth==0.5.0 -fastapi-mail==1.1.5 +fastapi-mail==1.2.2 h11==0.14.0 httpcore==0.16.2 httptools==0.5.0 @@ -24,6 +25,7 @@ MarkupSafe==2.1.1 orjson==3.8.3 passlib==1.7.4 pyasn1==0.4.8 +pycodestyle==2.10.0 pycparser==2.21 pydantic==1.10.2 PyJWT==1.7.1 @@ -35,7 +37,7 @@ rfc3986==1.5.0 rsa==4.9 six==1.16.0 sniffio==1.3.0 -starlette==0.22.0 +starlette==0.21.0 typing_extensions==4.4.0 ujson==5.6.0 uvicorn==0.20.0 From b4ddc2bb1662c3257cba04599febfa6b08cf896a Mon Sep 17 00:00:00 2001 From: CODEVO Date: Thu, 22 Dec 2022 06:20:49 +0000 Subject: [PATCH 12/14] updated --- Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9de29e1..33b1a11 100644 --- a/Makefile +++ b/Makefile @@ -2,4 +2,10 @@ dev: docker-compose up -d dev-down: - docker-compose down \ No newline at end of file + docker-compose down + +start-server: + uvicorn app.main:app --reload + +install-modules: + pip install fastapi[all] fastapi-mail==1.2.2 fastapi-jwt-auth[asymmetric] passlib[bcrypt] pymongo \ No newline at end of file From 2d9d01721ee7201ec355d59490429eb1c1da0eee Mon Sep 17 00:00:00 2001 From: CODEVO Date: Thu, 22 Dec 2022 17:25:54 +0000 Subject: [PATCH 13/14] updated --- requirements.txt | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/requirements.txt b/requirements.txt index 85c1279..c822130 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ aiosmtplib==1.1.7 anyio==3.6.2 -autopep8==2.0.1 bcrypt==4.0.1 blinker==1.5 certifi==2022.12.7 @@ -9,13 +8,12 @@ click==8.1.3 colorama==0.4.6 cryptography==3.4.8 dnspython==2.2.1 -ecdsa==0.18.0 email-validator==1.3.0 -fastapi==0.88.0 +fastapi==0.87.0 fastapi-jwt-auth==0.5.0 fastapi-mail==1.2.2 h11==0.14.0 -httpcore==0.16.2 +httpcore==0.16.3 httptools==0.5.0 httpx==0.23.1 idna==3.4 @@ -24,8 +22,6 @@ Jinja2==3.1.2 MarkupSafe==2.1.1 orjson==3.8.3 passlib==1.7.4 -pyasn1==0.4.8 -pycodestyle==2.10.0 pycparser==2.21 pydantic==1.10.2 PyJWT==1.7.1 @@ -34,7 +30,6 @@ python-dotenv==0.21.0 python-multipart==0.0.5 PyYAML==6.0 rfc3986==1.5.0 -rsa==4.9 six==1.16.0 sniffio==1.3.0 starlette==0.21.0 From 4c35770c6a74e5c39126fc06ce37d88ae72862a5 Mon Sep 17 00:00:00 2001 From: CODEVO Date: Thu, 29 Dec 2022 17:59:07 +0000 Subject: [PATCH 14/14] updated --- Makefile | 2 +- requirements.txt | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 33b1a11..3dfab37 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,4 @@ start-server: uvicorn app.main:app --reload install-modules: - pip install fastapi[all] fastapi-mail==1.2.2 fastapi-jwt-auth[asymmetric] passlib[bcrypt] pymongo \ No newline at end of file + pip install fastapi[all] fastapi-mail fastapi-jwt-auth[asymmetric] passlib[bcrypt] pymongo \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index c822130..c1009e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ -aiosmtplib==1.1.7 +aiosmtplib==2.0.0 anyio==3.6.2 bcrypt==4.0.1 +black==22.12.0 blinker==1.5 certifi==2022.12.7 cffi==1.15.1 @@ -9,9 +10,9 @@ colorama==0.4.6 cryptography==3.4.8 dnspython==2.2.1 email-validator==1.3.0 -fastapi==0.87.0 +fastapi==0.88.0 fastapi-jwt-auth==0.5.0 -fastapi-mail==1.2.2 +fastapi-mail==1.2.4 h11==0.14.0 httpcore==0.16.3 httptools==0.5.0 @@ -20,10 +21,13 @@ idna==3.4 itsdangerous==2.1.2 Jinja2==3.1.2 MarkupSafe==2.1.1 +mypy-extensions==0.4.3 orjson==3.8.3 passlib==1.7.4 +pathspec==0.10.3 +platformdirs==2.6.2 pycparser==2.21 -pydantic==1.10.2 +pydantic==1.10.3 PyJWT==1.7.1 pymongo==4.3.3 python-dotenv==0.21.0 @@ -32,7 +36,7 @@ PyYAML==6.0 rfc3986==1.5.0 six==1.16.0 sniffio==1.3.0 -starlette==0.21.0 +starlette==0.22.0 typing_extensions==4.4.0 ujson==5.6.0 uvicorn==0.20.0