Skip to content

Commit 5eb94a0

Browse files
authored
Merge pull request #97 from shinonomeow/shino_aio
fix: fix login error
2 parents 696cf16 + e05dfc2 commit 5eb94a0

5 files changed

Lines changed: 25 additions & 40 deletions

File tree

backend/README.md

Whitespace-only changes.

backend/src/module/api/auth.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,15 @@
2222
@router.post("/login", response_model=dict)
2323
async def login(response: Response, form_data=Depends(OAuth2PasswordRequestForm)):
2424
user = User(username=form_data.username, password=form_data.password)
25-
26-
from module.database import Database
27-
28-
with Database() as db:
29-
try:
30-
stored_user = db.user.get_user(user.username)
31-
except HTTPException:
32-
resp = ResponseModel(
33-
status_code=401,
34-
status=False,
35-
msg_en="User not found",
36-
msg_zh="用户不存在",
37-
)
38-
else:
39-
if not verify_password(user.password, stored_user.password):
40-
resp = ResponseModel(
41-
status_code=401,
42-
status=False,
43-
msg_en="Incorrect password",
44-
msg_zh="密码错误",
45-
)
46-
else:
47-
active_user.append(user.username)
48-
resp = ResponseModel(
49-
status_code=200,
50-
status=True,
51-
msg_en="Login successfully",
52-
msg_zh="登录成功",
53-
)
54-
55-
if resp.status:
25+
resp = auth_user(user)
26+
if not resp:
27+
resp = ResponseModel(
28+
status_code=401,
29+
status=False,
30+
msg_en="Incorrect username or password",
31+
msg_zh="用户名或密码错误",
32+
)
33+
else:
5634
token = create_access_token(
5735
data={"sub": user.username}, expires_delta=timedelta(days=1)
5836
)

backend/src/module/conf/log.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def setup_logger(level: int = logging.INFO, reset: bool = False):
5555
"hpack.hpack",
5656
"passlib",
5757
"multipart",
58+
"multipart.multipart",
5859
]
5960
for logger_name in loggers_to_silence:
6061
logger = logging.getLogger(logger_name)

backend/src/module/database/user.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class UserDatabase:
1313
def __init__(self, session: Session):
1414
self.session = session
1515

16-
def get_user(self, username):
16+
def get_user(self, username) -> User | None:
1717
statement = select(User).where(User.username == username)
1818
result = self.session.exec(statement).first()
19-
if not result:
20-
raise HTTPException(status_code=404, detail="User not found")
19+
# if not result:
20+
# raise HTTPException(status_code=404, detail="User not found")
2121
return result
2222

23-
def auth_user(self, user: User):
23+
def auth_user(self, user: User) -> bool:
2424
statement = select(User).where(User.username == user.username)
2525
result = self.session.exec(statement).first()
2626
if not result:

backend/src/module/security/api.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from module.database import Database
55
from module.models.user import User, UserUpdate
66

7-
from .jwt import verify_token
7+
from .jwt import verify_token,verify_password
88

99
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/auth/login")
1010

@@ -43,12 +43,18 @@ def update_user_info(user_data: UserUpdate, current_user):
4343
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
4444

4545

46-
def auth_user(user: User):
46+
def auth_user(user: User)->bool:
4747
with Database() as db:
48-
resp = db.user.auth_user(user)
49-
if resp:
48+
resp = db.user.get_user(user.username)
49+
50+
if not resp:
51+
return False
52+
if user.username == resp.username:
53+
res = verify_password(user.password, resp.password)
54+
if res:
5055
active_user.append(user.username)
51-
return resp
56+
return res
57+
return False
5258

5359

5460
UNAUTHORIZED = HTTPException(

0 commit comments

Comments
 (0)