update emails and verifing
This commit is contained in:
+85
-2
@@ -1,15 +1,24 @@
|
||||
# GEEN SQL IN MAIN.PY, DIT IS DE API, HOU HET OVERZICHTELIJK!!
|
||||
# GEEN SQL IN MAIN.PY, DIT IS DE API/ERMISSIECHECK, HOU HET OVERZICHTELIJK!!
|
||||
|
||||
from datetime import date
|
||||
from fastapi import FastAPI, Depends, HTTPException, Query
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import PlainTextResponse
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, EmailStr
|
||||
from typing import Literal
|
||||
import sqlite3
|
||||
from datetime import datetime, timezone
|
||||
from fastapi import Query
|
||||
|
||||
from database import fetch_one, execute
|
||||
from email_service import hash_token
|
||||
|
||||
from database import get_table_names
|
||||
|
||||
from email_service import (
|
||||
update_email
|
||||
)
|
||||
|
||||
from auth import (
|
||||
create_user,
|
||||
verify_user,
|
||||
@@ -59,6 +68,8 @@ from event_signups import (
|
||||
get_event_responses,
|
||||
)
|
||||
|
||||
class EmailUpdate(BaseModel):
|
||||
email: EmailStr
|
||||
|
||||
class MemberRoleUpdate(BaseModel):
|
||||
role: Literal["member", "admin"]
|
||||
@@ -976,6 +987,78 @@ def api_delete_event(
|
||||
"status": "deleted",
|
||||
}
|
||||
|
||||
# email endpoints
|
||||
|
||||
@app.put(
|
||||
"/me/email",
|
||||
summary="Update email address",
|
||||
description="Changes the authenticated user's email address.",
|
||||
)
|
||||
def api_update_email(
|
||||
data: EmailUpdate,
|
||||
user_id: int = Depends(get_current_user),
|
||||
):
|
||||
return update_email(
|
||||
user_id,
|
||||
data.email,
|
||||
)
|
||||
|
||||
@app.get("/verify-email")
|
||||
def verify_email(token: str = Query(...)):
|
||||
token_hash = hash_token(token)
|
||||
|
||||
verification = fetch_one(
|
||||
"""
|
||||
SELECT id, user_id, expires_at
|
||||
FROM email_verifications
|
||||
WHERE token_hash = ?
|
||||
""",
|
||||
(token_hash,)
|
||||
)
|
||||
|
||||
if not verification:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Invalid verification token"
|
||||
)
|
||||
|
||||
expires_at = datetime.fromisoformat(verification["expires_at"])
|
||||
|
||||
if expires_at < datetime.now(timezone.utc):
|
||||
execute(
|
||||
"""
|
||||
DELETE FROM email_verifications
|
||||
WHERE id = ?
|
||||
""",
|
||||
(verification["id"],)
|
||||
)
|
||||
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Verification token has expired"
|
||||
)
|
||||
|
||||
execute(
|
||||
"""
|
||||
UPDATE users
|
||||
SET email_verified = 1
|
||||
WHERE id = ?
|
||||
""",
|
||||
(verification["user_id"],)
|
||||
)
|
||||
|
||||
execute(
|
||||
"""
|
||||
DELETE FROM email_verifications
|
||||
WHERE id = ?
|
||||
""",
|
||||
(verification["id"],)
|
||||
)
|
||||
|
||||
return {
|
||||
"status": "verified",
|
||||
"message": "Email address successfully verified"
|
||||
}
|
||||
|
||||
# easter egg
|
||||
|
||||
|
||||
Reference in New Issue
Block a user