From 6a6afdbe72c626b01245c9372c9d10be79789bb0 Mon Sep 17 00:00:00 2001 From: Rasmus Luha Date: Sun, 6 Feb 2022 13:41:11 +0200 Subject: restrucurring the stucture of the folderstruture --> structure --- app/routers/auth.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app/routers/auth.py (limited to 'app/routers/auth.py') diff --git a/app/routers/auth.py b/app/routers/auth.py new file mode 100644 index 0000000..30668cf --- /dev/null +++ b/app/routers/auth.py @@ -0,0 +1,22 @@ +from fastapi import APIRouter, Depends, status, HTTPException, Response +from fastapi.security.oauth2 import OAuth2PasswordRequestForm +from sqlalchemy.orm import Session +from .. import database, schemas, models, utils, oauth2 + +router = APIRouter(tags = ["Authentication"]) + +@router.post("/login", response_model = schemas.Token) +def login(user_credentials: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(database.get_db)): #OAuth2Password... --> nüüd ei oota api requesti bodysse email, password vaid hoopis form-data. + + user = db.query(models.User).filter(models.User.email == user_credentials.username).first() + + if not user: + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Invalid Credentials unfortunatuun") + + if not utils.verify(user_credentials.password, user.password): + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN , detail="Invalid Credentials unfortunatuun") + + #Create and retrn token + access_token = oauth2.create_access_token(data = {"user_id":user.id}) + + return {"access_token" : access_token, "token_type" : "bearer" } -- cgit v1.2.3