add user email
This commit is contained in:
@@ -4,6 +4,7 @@ from ..models import users
|
||||
from typing import Annotated
|
||||
from bson import ObjectId
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi_mail import MessageSchema, MessageType
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -114,8 +115,22 @@ async def read_users_me(userSingle: users.UserCreate | None = None):
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Body request is empty"
|
||||
)
|
||||
fm = FastMail(mail.conf)
|
||||
user_repository = users.UserRepository(database=database.database)
|
||||
user = user_repository.find_one_by({"username": {'$eq': userSingle.username}})
|
||||
if user is not None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
detail="User is already exist"
|
||||
)
|
||||
|
||||
fm = FastMail(mail.conf)
|
||||
message = MessageSchema(
|
||||
subject="Fastapi-Mail module",
|
||||
recipients=userSingle.email,
|
||||
template_body=email.dict(),
|
||||
subtype=MessageType.html,
|
||||
)
|
||||
await fm.send_message(message, template_name="mailer.html")
|
||||
current_user = users.User(username=userSingle.username, password=user_token.get_password_hash(userSingle.password), email=userSingle.email)
|
||||
user_repository.save(current_user)
|
||||
return JSONResponse(status_code=200, content={"message": "email has been sent"})
|
Reference in New Issue
Block a user