Gregniuki commited on
Commit
5764b08
1 Parent(s): 59ac5b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -1
app.py CHANGED
@@ -87,7 +87,19 @@ async def auth_callback(request: Request, db: Session = Depends(get_db)):
87
  response = RedirectResponse(url="/protected")
88
  response.set_cookie(key="access_token", value=f"Bearer {access_token}", httponly=True)
89
  return response
90
-
 
 
 
 
 
 
 
 
 
 
 
 
91
  @app.get("/", response_class=HTMLResponse)
92
  async def landing(request: Request):
93
  return templates.TemplateResponse("landing.html", {"request": request})
 
87
  response = RedirectResponse(url="/protected")
88
  response.set_cookie(key="access_token", value=f"Bearer {access_token}", httponly=True)
89
  return response
90
+
91
+ @app.post("/login")
92
+ async def login(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)):
93
+ user = authenticate_user(db, form_data.username, form_data.password)
94
+ if not user:
95
+ raise HTTPException(
96
+ status_code=status.HTTP_401_UNAUTHORIZED,
97
+ detail="Incorrect username or password",
98
+ headers={"WWW-Authenticate": "Bearer"},
99
+ )
100
+ access_token = create_access_token(data={"sub": user.username})
101
+ return {"access_token": access_token, "token_type": "bearer"}
102
+
103
  @app.get("/", response_class=HTMLResponse)
104
  async def landing(request: Request):
105
  return templates.TemplateResponse("landing.html", {"request": request})