Init files
This commit is contained in:
24
auth/routers/authentication.py
Normal file
24
auth/routers/authentication.py
Normal file
@ -0,0 +1,24 @@
|
||||
from fastapi import APIRouter, Depends, status, HTTPException
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from auth import token
|
||||
from auth.login.models import login as models
|
||||
from databases.pgsql import database
|
||||
from auth.hashing import Hash
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
router = APIRouter(tags=['Authentication'])
|
||||
|
||||
|
||||
@router.post('/login')
|
||||
def login(request: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(database.get_db)):
|
||||
user = db.query(models.User).filter(
|
||||
models.User.email == request.username).first()
|
||||
if not user:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Invalid Credentials")
|
||||
if not Hash.verify(request.password,user.hashed_password):
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Incorrect password")
|
||||
|
||||
access_token = token.create_access_token(data={"sub": user.email})
|
||||
return {"access_token": access_token, "token_type": "bearer"}
|
Reference in New Issue
Block a user