|
|
|
|
|
from fastapi import FastAPI, Depends, HTTPException |
|
from fastapi.requests import Request |
|
from fastapi.responses import HTMLResponse |
|
import auth, tts |
|
from auth import verify_token |
|
|
|
app = FastAPI() |
|
router = APIRouter() |
|
|
|
|
|
|
|
app.include_router(auth.router, prefix="/auth") |
|
|
|
|
|
app.include_router(tts.router, prefix="/tts") |
|
|
|
|
|
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): |
|
|
|
return templates.TemplateResponse("verify.html", {"request": request}) |
|
|
|
|
|
@app.get("/protected", response_model=str) |
|
async def protected_route(self,request: Request, token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)): |
|
|
|
user = verify_token(token, self.SECRET_KEY, self.ALGORITHM) |
|
if user is None: |
|
raise HTTPException(status_code=401, detail="Invalid or expired token") |
|
|
|
|
|
db_user = get_user_by_email(db, user) |
|
|
|
if db_user is None: |
|
raise HTTPException(status_code=401, detail="User not found in the database") |
|
|
|
|
|
return templates.TemplateResponse("protected.html", {"request": request, "user": db_user.username}) |
|
|