Код: Выделить всё
@app.post("/login")
async def login(user: UserLogin):
connection = None
try:
connection = await get_db_connection()
except Exception as e:
logger.error(f"Connection error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
try:
async with connection.cursor() as cursor:
sql = "SELECT * FROM login WHERE email = %s"
values = (user.email,)
await cursor.execute(sql, values)
data = await cursor.fetchone()
if data:
data_dict = dict(data)
if bcrypt.checkpw(user.password.encode('utf-8'), data_dict['password'].encode('utf-8')):
token = create_access_token(data={"sub": data_dict['email']})
return {
"id": data_dict['id'],
"name": data_dict['name'],
"email": data_dict['email'],
"token": token
}
else:
raise HTTPException(status_code=401, detail="Invalid credentials")
else:
raise HTTPException(status_code=401, detail="Invalid credentials")
except Exception as e:
logger.error(f"Login error: {str(e)}")
raise HTTPException(status_code=500, detail="An error occurred during login")
finally:
if connection:
await connection.close()
Код: Выделить всё
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette\\middleware\\base.py", line 163, in call_next
raise app_exc
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette\\middleware\\base.py", line 149, in coro
await self.app(scope, receive_or_disconnect, send_no_error)
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette\\middleware\\cors.py", line 93, in __call__
await self.simple_response(scope, receive, send, request_headers=headers)
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette\\middleware\\cors.py", line 144, in simple_response
await self.app(scope, receive, send)
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette\\middleware\\exceptions.py", line 62, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette_exception_handler.py", line 62, in wrapped_app
raise exc
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette_exception_handler.py", line 51, in wrapped_app
await app(scope, receive, sender)
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette\\routing.py", line 715, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette\\routing.py", line 735, in app
await route.handle(scope, receive, send)
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette\\routing.py", line 288, in handle
await self.app(scope, receive, send)
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette\\routing.py", line 76, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette_exception_handler.py", line 62, in wrapped_app
raise exc
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette_exception_handler.py", line 51, in wrapped_app
await app(scope, receive, sender)
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\starlette\\routing.py", line 73, in app
response = await f(request)
^^^^^^^^^^^^^^^^
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\fastapi\\routing.py", line 301, in app
raw_response = await run_endpoint_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\Lirik\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\fastapi\\routing.py", line 212, in run_endpoint_function
return await dependant.call(\*\*values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\\Users\\Lirik\\Videos\\New folder\\backend\\server.py", line 210, in login
await connection.close()
TypeError: object NoneType can't be used in 'await' expression
Я попробовал добавить cors, но ничего не получилось.
Подробнее здесь: https://stackoverflow.com/questions/790 ... o-fix-this