diff --git a/backend/src/module/api/auth.py b/backend/src/module/api/auth.py index 613a3d1d..cada8e0e 100644 --- a/backend/src/module/api/auth.py +++ b/backend/src/module/api/auth.py @@ -4,6 +4,8 @@ from fastapi import APIRouter, Depends, HTTPException, status from fastapi.security import OAuth2PasswordRequestForm from fastapi.responses import JSONResponse, Response +from .response import u_response + from module.models.user import User, UserUpdate from module.models import APIResponse from module.security.api import ( @@ -20,13 +22,14 @@ router = APIRouter(prefix="/auth", tags=["auth"]) @router.post("/login", response_model=dict) async def login(response: Response, form_data=Depends(OAuth2PasswordRequestForm)): user = User(username=form_data.username, password=form_data.password) - auth_user(user) - token = create_access_token( - data={"sub": user.username}, expires_delta=timedelta(days=1) - ) - response.set_cookie(key="token", value=token, httponly=True, max_age=86400) - return {"access_token": token, "token_type": "bearer"} - + resp = auth_user(user) + if resp.status: + token = create_access_token( + data={"sub": user.username}, expires_delta=timedelta(days=1) + ) + response.set_cookie(key="token", value=token, httponly=True, max_age=86400) + return {"access_token": token, "token_type": "bearer"} + return u_response(resp) @router.get("/refresh_token", response_model=dict, dependencies=[Depends(get_current_user)]) async def refresh(response: Response): diff --git a/backend/src/module/api/response.py b/backend/src/module/api/response.py index 332d91ee..b0a2dc26 100644 --- a/backend/src/module/api/response.py +++ b/backend/src/module/api/response.py @@ -8,7 +8,6 @@ def u_response(response_model: ResponseModel): return JSONResponse( status_code=response_model.status_code, content={ - "status": response_model.status, "msg_en": response_model.msg_en, "msg_zh": response_model.msg_zh, }, diff --git a/backend/src/module/database/user.py b/backend/src/module/database/user.py index 62bc8ba2..fdfa6464 100644 --- a/backend/src/module/database/user.py +++ b/backend/src/module/database/user.py @@ -3,6 +3,7 @@ import logging from fastapi import HTTPException from module.models.user import User, UserUpdate, UserLogin +from module.models import ResponseModel from module.security.jwt import get_password_hash, verify_password from sqlmodel import Session, select @@ -20,14 +21,29 @@ class UserDatabase: raise HTTPException(status_code=404, detail="User not found") return result - def auth_user(self, user: User) -> bool: + def auth_user(self, user: User): statement = select(User).where(User.username == user.username) result = self.session.exec(statement).first() if not result: - raise HTTPException(status_code=401, detail="User not found") + return ResponseModel( + status_code=401, + status=False, + msg_en="User not found", + msg_zh="用户不存在" + ) if not verify_password(user.password, result.password): - raise HTTPException(status_code=401, detail="Password error") - return True + return ResponseModel( + status_code=401, + status=False, + msg_en="Incorrect password", + msg_zh="密码错误" + ) + return ResponseModel( + status_code=200, + status=True, + msg_en="Login successfully", + msg_zh="登录成功" + ) def update_user(self, username, update_user: UserUpdate): # Update username and password diff --git a/backend/src/module/security/api.py b/backend/src/module/security/api.py index b5647b14..3b8cc510 100644 --- a/backend/src/module/security/api.py +++ b/backend/src/module/security/api.py @@ -45,9 +45,10 @@ def update_user_info(user_data: UserUpdate, current_user): def auth_user(user: User): with Database() as db: - if db.user.auth_user(user): + resp = db.user.auth_user(user) + if resp.status: active_user.append(user.username) - return True + return resp UNAUTHORIZED = HTTPException( diff --git a/webui/src/hooks/useAuth.ts b/webui/src/hooks/useAuth.ts index e932872c..3016b5bb 100644 --- a/webui/src/hooks/useAuth.ts +++ b/webui/src/hooks/useAuth.ts @@ -48,8 +48,8 @@ export const useAuth = createSharedComposable(() => { if (error.status === 404) { message.error('请更新AutoBangumi!'); - } else { - message.error(error.msg_zh); + } else if (error.status === 401){ + message.error(err.msg_zh); } }); diff --git a/webui/src/pages/login.vue b/webui/src/pages/login.vue index 55d4c43d..5268e287 100644 --- a/webui/src/pages/login.vue +++ b/webui/src/pages/login.vue @@ -38,9 +38,5 @@ definePage({ - - - - diff --git a/webui/src/utils/axios.ts b/webui/src/utils/axios.ts index 432f973b..9b6a3ad0 100644 --- a/webui/src/utils/axios.ts +++ b/webui/src/utils/axios.ts @@ -1,5 +1,5 @@ import Axios from 'axios'; -import type { ApiError } from "#/api"; +import type {ApiError} from "#/api"; export const axios = Axios.create(); @@ -17,38 +17,38 @@ export const axios = Axios.create(); axios.defaults.withCredentials = true; axios.interceptors.response.use( - (res) => { - return res; - }, - (err) => { - const status = err.response.status as ApiError['status']; - const msg_en = (err.response.data.msg_en ?? '') as ApiError['msg_en']; - const msg_zh = (err.response.data.msg_zh ?? '') as ApiError['msg_zh']; + (res) => { + return res; + }, + (err) => { + const status = err.response.status as ApiError['status']; + const msg_en = (err.response.data.msg_en ?? '') as ApiError['msg_en']; + const msg_zh = (err.response.data.msg_zh ?? '') as ApiError['msg_zh']; - const error = { - status, - msg_en, - msg_zh, - }; + const error = { + status, + msg_en, + msg_zh, + }; - const message = useMessage(); + const message = useMessage(); - /** token 过期 */ - if (error.status === 401) { - const { auth } = useAuth(); - auth.value = ''; + /** token 过期 */ + if (error.status === 401) { + const {auth} = useAuth(); + auth.value = ''; + } + + /** 执行失败 */ + if (error.status === 406) { + message.error(error.msg_zh); + } + + if (error.status === 500) { + const msg = (err.response.data.msg_en ?? '') as ApiError['msg_en'] + message.error(msg); + } + + return Promise.reject(error); } - - /** 执行失败 */ - if (error.status === 406) { - message.error(error.msg_zh); - } - - if (error.status === 500) { - const msg = (err.response.data.msg_en ?? '') as ApiError['msg_en'] - message.error(msg); - } - - return Promise.reject(error); - } );