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', '登录失败') + } }) }