Loginauth / main.py
Gregniuki's picture
Update main.py
8268a41
raw
history blame
4.52 kB
#main.py
from fastapi import FastAPI, Form, Depends, HTTPException, APIRouter
from fastapi.requests import Request
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.templating import Jinja2Templates
from sqlalchemy.orm import Session
from auth import verify_token, oauth2_scheme, auth_views, register, UserCreate, verify_email
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="")
# 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
# Route for the landing page
@app.get("/", response_class=HTMLResponse)
async def landing(request: Request):
return templates.TemplateResponse("landing.html", {"request": request})
# Your other routes and app configuration go here
@app.get("/login", response_class=HTMLResponse)
async def login(request: Request):
return templates.TemplateResponse("login.html", {"request": request})
@app.post("/login", response_class=HTMLResponse)
async def login_post(
request: Request,
email: str = Form(...),
password: str = Form(...),
db: Session = Depends(get_db)
):
# Validate the email and password
if not email or not password:
raise HTTPException(status_code=400, detail="Invalid email or password")
# Check user authentication (You should implement this function)
user = authenticate_user(db, email, password)
if user:
# Authentication succeeded
# Generate an access token (You should implement this function)
access_token = create_access_token(user.email)
# Redirect to a protected route with the access token
return RedirectResponse("/protected?token=" + access_token)
else:
# Authentication failed
# You can redirect back to the login page with an error message
return RedirectResponse("/login?error=Authentication failed")
@app.get("/register", response_class=HTMLResponse)
async def register_get(request: Request):
return templates.TemplateResponse("register.html", {"request": request})
@app.post("/register", response_class=HTMLResponse)
async def register_post(
request: Request,
username: str = Form(...),
email: str = Form(...),
password: str = Form(...),
confirm_password: str = Form(...),
db: Session = Depends(get_db)
):
user = UserCreate(username=username, email=email, password=password, confirm_password=confirm_password)
registered_user = register(user, db)
return RedirectResponse("/registration_successful")
@app.get("/registration_successful", response_class=HTMLResponse)
async def registration_successful(request: Request):
return templates.TemplateResponse("registration_successful.html", {"request": request})
@app.get("/verify/{verification_token}", response_class=HTMLResponse)
async def verify_email(verification_token: str, request: Request, db: Session = Depends(get_db)):
# Perform email verification
verification_result = verify_email(verification_token, db)
# Handle a successful verification
if "message" in verification_result:
# Redirect the user to the protected area
return RedirectResponse("/protected")
# Handle any other cases, such as errors
return HTTPException(status_code=400, detail="Verification failed")
# User authentication (protected route)
@app.post("/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})