chore: 娣诲姞 .gitignore 娓呯悊缂撳瓨鏂囦欢鍜屾暟鎹簱
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
e76e856dba
commit
dba4c55322
20
.gitignore
vendored
Normal file
20
.gitignore
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
# Python
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.pyo
|
||||
*.db
|
||||
|
||||
# Node
|
||||
node_modules/
|
||||
frontend/dist/
|
||||
|
||||
# IDE
|
||||
.vscode/
|
||||
.idea/
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Env
|
||||
.env
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -38,10 +38,11 @@ def get_current_user(token: str = Depends(oauth2_scheme), db: Session = Depends(
|
||||
)
|
||||
try:
|
||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||
user_id: int = payload.get("sub")
|
||||
if user_id is None:
|
||||
sub = payload.get("sub")
|
||||
if sub is None:
|
||||
raise credentials_exception
|
||||
except JWTError:
|
||||
user_id = int(sub)
|
||||
except (JWTError, ValueError, TypeError):
|
||||
raise credentials_exception
|
||||
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -16,7 +16,7 @@ def login(req: LoginRequest, db: Session = Depends(get_db)):
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="用户名或密码错误")
|
||||
if not user.is_active:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="账号已停用")
|
||||
token = create_access_token(data={"sub": user.id})
|
||||
token = create_access_token(data={"sub": str(user.id)})
|
||||
return {"access_token": token, "token_type": "bearer"}
|
||||
|
||||
|
||||
|
||||
3
frontend/.vscode/extensions.json
vendored
3
frontend/.vscode/extensions.json
vendored
@ -1,3 +0,0 @@
|
||||
{
|
||||
"recommendations": ["Vue.volar"]
|
||||
}
|
||||
@ -22,9 +22,12 @@ api.interceptors.response.use(
|
||||
err => {
|
||||
const msg = err.response?.data?.detail || '请求失败'
|
||||
if (err.response?.status === 401) {
|
||||
const isOnLogin = window.location.pathname === '/login'
|
||||
localStorage.removeItem('token')
|
||||
if (!isOnLogin) {
|
||||
router.push('/login')
|
||||
ElMessage.error('登录已过期,请重新登录')
|
||||
}
|
||||
} else {
|
||||
ElMessage.error(msg)
|
||||
}
|
||||
|
||||
@ -27,7 +27,12 @@ const router = createRouter({
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const token = localStorage.getItem('token')
|
||||
if (to.meta.public) {
|
||||
// 已登录时访问登录页,直接跳首页
|
||||
if (to.path === '/login' && token) {
|
||||
next('/')
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
} else if (!token) {
|
||||
next('/login')
|
||||
} else {
|
||||
|
||||
@ -10,10 +10,16 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
const res = await authApi.login({ username, password })
|
||||
token.value = res.access_token
|
||||
localStorage.setItem('token', res.access_token)
|
||||
await fetchUser()
|
||||
// 登录后立即获取用户信息,失败不影响登录流程
|
||||
try {
|
||||
user.value = await authApi.me()
|
||||
} catch (e) {
|
||||
console.error('fetchUser after login failed:', e)
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchUser() {
|
||||
if (!token.value) return
|
||||
try {
|
||||
user.value = await authApi.me()
|
||||
} catch {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user