I am creating a basic login form using oauth2.0 in FastAPI and MongoDB. I am trying to get the email and password from the database but it keeps showing the same error "'id': str(user['_id']), TypeError: string indices must be integers". I am not exactly sure what I am doing wrong.
@authentication.post('/login')
async def login(form_email: OAuth2PasswordRequestForm = Depends(),
form_password: OAuth2PasswordRequestForm = Depends()):
email = users_serializer(user_list.find_one({"email": form_email.username}))
password = users_serializer(user_list.find_one({"password": form_password.password}))
print(email)
print(password)
if form_email.username == email:
if form_password.password == password:
return {"status": "ok", "details": f"Welcome! {form_email.username} "}
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='Incorrect email or password')
This is the users_serializer schema:
def user_serializer(user) -> dict:
return {
'id': str(user['_id']),
'name': str(user['name']),
'email': str(user['email']),
'password': str(user['password']),
}
Can anyone help me understand what I am doing wrong? Thanks in advance!
PS: I don't have much experience with FastAPI and MongoDB.