Gregniuki commited on
Commit
5ed4a12
1 Parent(s): aa6aa93

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +7 -10
main.py CHANGED
@@ -2,7 +2,7 @@
2
 
3
  from fastapi import FastAPI, Form, Depends, HTTPException
4
  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, authenticate_user, get_user_by_verification_token
@@ -123,26 +123,23 @@ async def registration_successful(request: Request):
123
 
124
 
125
 
126
-
127
  @app.get("/verify/{verification_token}", response_class=HTMLResponse)
128
- async def verify_email(verification_token: str, request: Request, db: Session = Depends(get_db)):
129
- # Verify the email using the token
130
  user = get_user_by_verification_token(db, verification_token)
131
-
132
  if not user:
133
  raise HTTPException(status_code=400, detail="Invalid verification token")
134
 
135
  if user.is_verified:
136
  raise HTTPException(status_code=400, detail="Email already verified")
137
 
138
- # Mark the email as verified in the database
139
  user.is_verified = True
140
- #user.email_verification_token = None # Optionally clear the verification token
141
  db.commit()
142
 
143
- # Handle a successful verification
144
- # return templates.TemplateResponse("verification_successful.html", {"request": request})
145
- return RedirectResponse("/protected")
 
146
 
147
  # User authentication (protected route)
148
  @app.post("/protected", response_class=HTMLResponse) # Specify response_class as HTMLResponse
 
2
 
3
  from fastapi import FastAPI, Form, Depends, HTTPException
4
  from fastapi.requests import Request
5
+ from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse
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
 
123
 
124
 
125
 
 
126
  @app.get("/verify/{verification_token}", response_class=HTMLResponse)
127
+ async def verify_email(verification_token: str, db: Session = Depends(get_db)):
 
128
  user = get_user_by_verification_token(db, verification_token)
 
129
  if not user:
130
  raise HTTPException(status_code=400, detail="Invalid verification token")
131
 
132
  if user.is_verified:
133
  raise HTTPException(status_code=400, detail="Email already verified")
134
 
 
135
  user.is_verified = True
136
+ user.email_verification_token = None # Clear the verification token
137
  db.commit()
138
 
139
+ # Create access token for the user after successful verification
140
+ access_token = auth_views.create_access_token(data={"sub": user.email}, expires_delta=timedelta(minutes=auth_views.ACCESS_TOKEN_EXPIRE_MINUTES))
141
+ # Redirect to the protected route with the token as a query parameter (or as required by your front-end/client)
142
+ return RedirectResponse(url=f"/protected?token={access_token}")
143
 
144
  # User authentication (protected route)
145
  @app.post("/protected", response_class=HTMLResponse) # Specify response_class as HTMLResponse