File size: 2,273 Bytes
763951b
 
325f578
 
44372ef
325f578
 
 
 
 
 
10b597c
 
763951b
325f578
10b597c
625461f
763951b
625461f
 
763951b
10b597c
625461f
 
 
 
 
325f578
44372ef
 
 
 
 
 
 
 
 
 
 
 
c209774
325f578
c209774
325f578
c209774
673cb74
c209774
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#main.py

from fastapi import FastAPI, Depends, HTTPException, APIRouter
from fastapi.requests import Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from sqlalchemy.orm import Session
from auth import verify_token, oauth2_scheme
from database import get_db, get_user_by_email
import auth
import tts

app = FastAPI()
#router = APIRouter()
templates = Jinja2Templates(directory="templates")

# Include the authentication router with the prefix '/auth'
app.include_router(auth.router, prefix="/auth")

# Include the TTS router with the prefix '/tts'
app.include_router(tts.router, prefix="/tts")

# Dependency for verifying the user's token
def get_current_user(token: str = Depends(verify_token)):
    if not token:
        raise HTTPException(status_code=401, detail="Token not valid")
    return token

@app.get("/login", response_class=HTMLResponse)
async def login(request: Request):
    return templates.TemplateResponse("login.html", {"request": request})

@app.get("/register", response_class=HTMLResponse)
async def register(request: Request):
    return templates.TemplateResponse("register.html", {"request": request})

@app.get("/verify/{verification_token}", response_class=HTMLResponse)
async def verify_email(verification_token: str, request: Request):
    # Perform verification and return an appropriate template
    return templates.TemplateResponse("verify.html", {"request": request})

# User authentication (protected route)
@app.get("/protected", response_model=str)
async def protected_route(request: Request, token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
    # Verify the access token
    user = verify_token(token, my_secret_key, "HS256")
    if user is None:
        raise HTTPException(status_code=401, detail="Invalid or expired token")

    # Check if the user exists in the database
    db_user = get_user_by_email(db, user)  # Modify this to match your database query

    if db_user is None:
        raise HTTPException(status_code=401, detail="User not found in the database")

    # The user exists in the database, and you can render the protected route template
    return templates.TemplateResponse("protected.html", {"request": request, "user": db_user.username})