Update main.py
Browse files
main.py
CHANGED
@@ -33,3 +33,20 @@ async def register(request: Request):
|
|
33 |
async def verify_email(verification_token: str, request: Request):
|
34 |
# Perform verification and return an appropriate template
|
35 |
return templates.TemplateResponse("verify.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
async def verify_email(verification_token: str, request: Request):
|
34 |
# Perform verification and return an appropriate template
|
35 |
return templates.TemplateResponse("verify.html", {"request": request})
|
36 |
+
|
37 |
+
# User authentication (protected route)
|
38 |
+
@app.get("/protected", response_model=str)
|
39 |
+
async def protected_route(self,request: Request, token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
|
40 |
+
# Verify the access token
|
41 |
+
user = verify_token(token, self.SECRET_KEY, self.ALGORITHM)
|
42 |
+
if user is None:
|
43 |
+
raise HTTPException(status_code=401, detail="Invalid or expired token")
|
44 |
+
|
45 |
+
# Check if the user exists in the database
|
46 |
+
db_user = get_user_by_email(db, user) # Modify this to match your database query
|
47 |
+
|
48 |
+
if db_user is None:
|
49 |
+
raise HTTPException(status_code=401, detail="User not found in the database")
|
50 |
+
|
51 |
+
# The user exists in the database, and you can render the protected route template
|
52 |
+
return templates.TemplateResponse("protected.html", {"request": request, "user": db_user.username})
|