Gregniuki commited on
Commit
dc39372
1 Parent(s): 92a1e0e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +9 -9
main.py CHANGED
@@ -5,7 +5,7 @@ from fastapi.requests import Request
5
  from fastapi.responses import HTMLResponse, RedirectResponse
6
  from fastapi.templating import Jinja2Templates
7
  from sqlalchemy.orm import Session
8
- from auth import verify_token, oauth2_scheme, auth_views, register, UserCreate, verify_email, authenticate_user
9
  from database import get_db, get_user_by_email
10
  #import auth
11
  #import tts
@@ -92,26 +92,26 @@ async def registration_successful(request: Request):
92
 
93
 
94
 
 
 
95
  @app.get("/verify/{verification_token}", response_class=HTMLResponse)
96
  async def verify_email(verification_token: str, request: Request, db: Session = Depends(get_db)):
97
  # Verify the email using the token
98
- user_email = verify_token(verification_token)
99
- if not user_email:
100
- return HTTPException(status_code=400, detail="Invalid verification token")
101
-
102
- # Get the user by email
103
- user = get_user_by_email(db, user_email)
104
  if not user:
105
- return HTTPException(status_code=400, detail="User not found")
106
 
107
  if user.is_verified:
108
- return HTTPException(status_code=400, detail="Email already verified")
109
 
110
  # Mark the email as verified in the database
111
  user.is_verified = True
 
112
  db.commit()
113
 
114
  # Handle a successful verification
 
115
  return RedirectResponse("/protected")
116
 
117
  # User authentication (protected route)
 
5
  from fastapi.responses import HTMLResponse, RedirectResponse
6
  from fastapi.templating import Jinja2Templates
7
  from sqlalchemy.orm import Session
8
+ from auth import verify_token, oauth2_scheme, auth_views, register, UserCreate, authenticate_user, get_user_by_verification_token
9
  from database import get_db, get_user_by_email
10
  #import auth
11
  #import tts
 
92
 
93
 
94
 
95
+
96
+
97
  @app.get("/verify/{verification_token}", response_class=HTMLResponse)
98
  async def verify_email(verification_token: str, request: Request, db: Session = Depends(get_db)):
99
  # Verify the email using the token
100
+ user = get_user_by_verification_token(db, verification_token)
101
+
 
 
 
 
102
  if not user:
103
+ raise HTTPException(status_code=400, detail="Invalid verification token")
104
 
105
  if user.is_verified:
106
+ raise HTTPException(status_code=400, detail="Email already verified")
107
 
108
  # Mark the email as verified in the database
109
  user.is_verified = True
110
+ user.email_verification_token = None # Optionally clear the verification token
111
  db.commit()
112
 
113
  # Handle a successful verification
114
+ # return templates.TemplateResponse("verification_successful.html", {"request": request})
115
  return RedirectResponse("/protected")
116
 
117
  # User authentication (protected route)