From 6e9c213893062b873f844a38efa588db772d6651 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Wed, 20 Nov 2024 21:03:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=99=BB=E5=BD=95=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E6=97=B6=E6=97=A0=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/api/http/controller/groups/user.py | 6 +++++- pkg/api/http/service/user.py | 3 +-- web/src/components/LoginDialog.vue | 10 +++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/api/http/controller/groups/user.py b/pkg/api/http/controller/groups/user.py index 9bc8bf74..ce8e7448 100644 --- a/pkg/api/http/controller/groups/user.py +++ b/pkg/api/http/controller/groups/user.py @@ -1,5 +1,6 @@ import quart import sqlalchemy +import argon2 from .. import group from .....persistence.entities import user @@ -32,7 +33,10 @@ async def _() -> str: async def _() -> str: json_data = await quart.request.json - token = await self.ap.user_service.authenticate(json_data['user'], json_data['password']) + try: + token = await self.ap.user_service.authenticate(json_data['user'], json_data['password']) + except argon2.exceptions.VerifyMismatchError: + return self.fail(1, '用户名或密码错误') return self.success(data={ 'token': token diff --git a/pkg/api/http/service/user.py b/pkg/api/http/service/user.py index b1d00a73..93774778 100644 --- a/pkg/api/http/service/user.py +++ b/pkg/api/http/service/user.py @@ -51,8 +51,7 @@ async def authenticate(self, user_email: str, password: str) -> str | None: ph = argon2.PasswordHasher() - if not ph.verify(user_obj.password, password): - raise ValueError('密码错误') + ph.verify(user_obj.password, password) return await self.generate_jwt_token(user_email) diff --git a/web/src/components/LoginDialog.vue b/web/src/components/LoginDialog.vue index 4cfa50a6..087bb3e7 100644 --- a/web/src/components/LoginDialog.vue +++ b/web/src/components/LoginDialog.vue @@ -47,17 +47,21 @@ const login = () => { user: user.value, password: password.value }).then(res => { - if (res.data.data.token) { + if (res.data.code == 0) { emit('success', '登录成功') localStorage.setItem('user-token', res.data.data.token) setTimeout(() => { location.reload() }, 1000) } else { - emit('error', '登录失败') + emit('error', res.data.msg) } }).catch(err => { - emit('error', err.response.data.message) + if (err.response.data.msg) { + emit('error', err.response.data.msg) + } else { + emit('error', '登录失败') + } }) }