mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-04-13 18:11:03 +08:00
fix: login failed message.
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -38,9 +38,5 @@ definePage({
|
||||
</div>
|
||||
</div>
|
||||
</ab-container>
|
||||
|
||||
<!-- <div bg="#C7C4AB" text-white rounded-4px py-4px px-2em text-main> -->
|
||||
<!-- <div>{{ $t('login.default') }}: admin adminadmin</div> -->
|
||||
<!-- </div> -->
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user