add confirm mail #47
@ -1,12 +1,18 @@
|
|||||||
from fastapi import APIRouter, HTTPException, status
|
from fastapi import APIRouter, HTTPException, status, Request
|
||||||
|
from fastapi.templating import Jinja2Templates
|
||||||
from ..dependencies import users_token, database, mail
|
from ..dependencies import users_token, database, mail
|
||||||
from ..models import users, email
|
from ..models import users, email
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse, HTMLResponse
|
||||||
from fastapi_mail import MessageSchema, MessageType, FastMail
|
from fastapi_mail import MessageSchema, MessageType, FastMail
|
||||||
import random
|
import random, os
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
# Assurer que le chemin vers "templates" est correct
|
||||||
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
templates = Jinja2Templates(directory=os.path.join(BASE_DIR, "templates"))
|
||||||
|
|
||||||
|
|
||||||
@router.post("/mail",tags=["mail"])
|
@router.post("/mail",tags=["mail"])
|
||||||
async def create_user(userSingle: users.UserCreate | None = None):
|
async def create_user(userSingle: users.UserCreate | None = None):
|
||||||
if userSingle is None:
|
if userSingle is None:
|
||||||
@ -41,21 +47,26 @@ async def create_user(userSingle: users.UserCreate | None = None):
|
|||||||
database.connect_redis.set(userSingle.username, key_hashed)
|
database.connect_redis.set(userSingle.username, key_hashed)
|
||||||
return JSONResponse(status_code=status.HTTP_200_OK, content={"message": "email has been sent"})
|
return JSONResponse(status_code=status.HTTP_200_OK, content={"message": "email has been sent"})
|
||||||
|
|
||||||
@router.get("/mail",tags=["mail"])
|
@router.get("/mail", tags=["mail"])
|
||||||
async def confirm_user(key: str | None = None, username: str | None = None):
|
async def confirm_user(request: Request, key: str | None = None, username: str | None = None):
|
||||||
if key is None or username is None:
|
if key is None or username is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
detail="Parameter key or/and username is empty"
|
detail="Parameter key or/and username is empty"
|
||||||
)
|
)
|
||||||
|
|
||||||
user_repository = users.UserRepository(database=database.database)
|
user_repository = users.UserRepository(database=database.database)
|
||||||
user = user_repository.find_one_by({"username": {'$eq': username}})
|
user = user_repository.find_one_by({"username": {'$eq': username}})
|
||||||
key_hashed = database.connect_redis.get(username)
|
key_hashed = database.connect_redis.get(username)
|
||||||
|
|
||||||
if key_hashed != key:
|
if key_hashed != key:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
detail="Key is invalid"
|
detail="Key is invalid"
|
||||||
)
|
)
|
||||||
|
|
||||||
user.status = 1
|
user.status = 1
|
||||||
user_repository.save(user)
|
user_repository.save(user)
|
||||||
return JSONResponse(status_code=status.HTTP_200_OK, content={"message": "user account confirmed"})
|
|
||||||
|
# Rendre la page HTML avec Jinja2 et passer la variable username
|
||||||
|
return templates.TemplateResponse("confirm.html", {"request": request, "username": username})
|
58
app/templates/confirm.html
Normal file
58
app/templates/confirm.html
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Votre compte est activé</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
background-color: #f4f4f4;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
max-width: 600px;
|
||||||
|
margin: 20px auto;
|
||||||
|
background: #ffffff;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 10px;
|
||||||
|
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
h2 {
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
p {
|
||||||
|
color: #666;
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
.button {
|
||||||
|
display: inline-block;
|
||||||
|
background: #007BFF;
|
||||||
|
color: white;
|
||||||
|
padding: 12px 20px;
|
||||||
|
text-decoration: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: bold;
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
.button:hover {
|
||||||
|
background: #0056b3;
|
||||||
|
}
|
||||||
|
.footer {
|
||||||
|
margin-top: 20px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: #888;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h2>Félicitations, {{ username }} ! 🎉</h2>
|
||||||
|
<p>Votre compte a été activé avec succès.</p>
|
||||||
|
<p>Vous pouvez maintenant vous connecter et profiter pleinement de nos services.</p>
|
||||||
|
<p class="footer">Si vous avez des questions, n'hésitez pas à nous contacter.</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,6 +1,59 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head><title>Email</title></head>
|
<head>
|
||||||
<body><p>Voici un lien https://localhost:8080/api/mail?key={{ key }}&username={{ username }}
|
<meta charset="UTF-8">
|
||||||
</p></body>
|
<title>Confirmation de votre compte</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
background-color: #f4f4f4;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
max-width: 600px;
|
||||||
|
margin: 20px auto;
|
||||||
|
background: #ffffff;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 10px;
|
||||||
|
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
h2 {
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
p {
|
||||||
|
color: #666;
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
.button {
|
||||||
|
display: inline-block;
|
||||||
|
background: #28a745;
|
||||||
|
color: white;
|
||||||
|
padding: 12px 20px;
|
||||||
|
text-decoration: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: bold;
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
.button:hover {
|
||||||
|
background: #218838;
|
||||||
|
}
|
||||||
|
.footer {
|
||||||
|
margin-top: 20px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: #888;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h2>Bienvenue, {{ username }} ! 🎉</h2>
|
||||||
|
<p>Merci de vous être inscrit sur notre plateforme.</p>
|
||||||
|
<p>Pour finaliser votre inscription, veuillez confirmer votre compte en cliquant sur le bouton ci-dessous :</p>
|
||||||
|
<a href="https://backend.valczeryba.ovh/api/confirm?key={{ key }}&username={{ username }}" class="button">Confirmer mon compte</a>
|
||||||
|
<p class="footer">Si vous n'êtes pas à l'origine de cette inscription, ignorez simplement cet email.</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user