Add POST endpoint for /login
This commit is contained in:
parent
c4562386a6
commit
83140043a1
@ -16,22 +16,21 @@ async def login(
|
|||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
user = db.query(User).filter(User.email == login_data.email).first()
|
user = db.query(User).filter(User.email == login_data.email).first()
|
||||||
if not user:
|
if not user or not verify_password(login_data.password, user.hashed_password):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
detail="Incorrect email or password"
|
detail="Incorrect email or password",
|
||||||
)
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
|
|
||||||
if not verify_password(login_data.password, user.hashed_password):
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
||||||
detail="Incorrect email or password"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
access_token = create_access_token(data={"sub": user.email})
|
access_token = create_access_token(data={"sub": user.email})
|
||||||
return {
|
return {
|
||||||
"access_token": access_token,
|
"access_token": access_token,
|
||||||
"token_type": "bearer",
|
"token_type": "bearer",
|
||||||
"user": UserSchema.from_orm(user)
|
"user": {
|
||||||
|
"id": user.id,
|
||||||
|
"email": user.email,
|
||||||
|
"role": user.role
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
Loading…
x
Reference in New Issue
Block a user