Gregniuki commited on
Commit
8268a41
1 Parent(s): c5c4c92

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -5
main.py CHANGED
@@ -36,16 +36,31 @@ async def landing(request: Request):
36
  async def login(request: Request):
37
  return templates.TemplateResponse("login.html", {"request": request})
38
 
 
39
  @app.post("/login", response_class=HTMLResponse)
40
- async def register_post(
41
  request: Request,
42
  email: str = Form(...),
43
  password: str = Form(...),
44
  db: Session = Depends(get_db)
45
  ):
46
-
47
- return RedirectResponse("/protected")
48
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  @app.get("/register", response_class=HTMLResponse)
51
  async def register_get(request: Request):
@@ -83,7 +98,7 @@ async def verify_email(verification_token: str, request: Request, db: Session =
83
  return HTTPException(status_code=400, detail="Verification failed")
84
 
85
  # User authentication (protected route)
86
- @app.get("/protected", response_model=str)
87
  async def protected_route(request: Request, token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
88
  # Verify the access token
89
  user = verify_token(token, my_secret_key, "HS256")
 
36
  async def login(request: Request):
37
  return templates.TemplateResponse("login.html", {"request": request})
38
 
39
+
40
  @app.post("/login", response_class=HTMLResponse)
41
+ async def login_post(
42
  request: Request,
43
  email: str = Form(...),
44
  password: str = Form(...),
45
  db: Session = Depends(get_db)
46
  ):
47
+ # Validate the email and password
48
+ if not email or not password:
49
+ raise HTTPException(status_code=400, detail="Invalid email or password")
50
+
51
+ # Check user authentication (You should implement this function)
52
+ user = authenticate_user(db, email, password)
53
+
54
+ if user:
55
+ # Authentication succeeded
56
+ # Generate an access token (You should implement this function)
57
+ access_token = create_access_token(user.email)
58
+ # Redirect to a protected route with the access token
59
+ return RedirectResponse("/protected?token=" + access_token)
60
+ else:
61
+ # Authentication failed
62
+ # You can redirect back to the login page with an error message
63
+ return RedirectResponse("/login?error=Authentication failed")
64
 
65
  @app.get("/register", response_class=HTMLResponse)
66
  async def register_get(request: Request):
 
98
  return HTTPException(status_code=400, detail="Verification failed")
99
 
100
  # User authentication (protected route)
101
+ @app.post("/protected", response_model=str)
102
  async def protected_route(request: Request, token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
103
  # Verify the access token
104
  user = verify_token(token, my_secret_key, "HS256")