Skip to content

Commit 0b366a7

Browse files
committed
fix
1 parent 8ccc1cc commit 0b366a7

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/App.jsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Container, Flex, Text, HStack, VStack, Box } from "@chakra-ui/react";
22
import { Icon } from "@iconify/react";
3-
import { Routes, Route } from "react-router-dom";
4-
import { Suspense } from "react";
3+
import { Routes, Route, useNavigate } from "react-router-dom";
4+
import { Suspense, useEffect } from "react";
55
import { motion } from "framer-motion";
66
import "./index.css";
77
import { ProfileImage } from "./components/ProfileImage";
@@ -110,6 +110,32 @@ function LoadingScreen() {
110110
}
111111

112112
function App() {
113+
const navigate = useNavigate();
114+
115+
// 在任意页面挂载时检查 OAuth 回调(GitHub 只能回调到根路径)
116+
useEffect(() => {
117+
const params = new URLSearchParams(window.location.search);
118+
const code = params.get("code");
119+
const state = params.get("state");
120+
if (code && state) {
121+
import("./services/zoneService").then(({ githubService }) => {
122+
githubService
123+
.handleOAuthCallback(code, state)
124+
.then((ok) => {
125+
// 清理 URL 参数
126+
window.history.replaceState({}, document.title, "/");
127+
if (ok) {
128+
// 回调成功后跳转到 /zone
129+
navigate("/zone", { replace: true });
130+
}
131+
})
132+
.catch(() => {
133+
window.history.replaceState({}, document.title, "/");
134+
});
135+
});
136+
}
137+
}, [navigate]);
138+
113139
return (
114140
<Box minH="100vh" bg="#8FC4FF" position="relative" zIndex={1}>
115141
<Suspense fallback={<LoadingScreen />}>

src/pages/ZonePage.jsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,21 @@ function ZonePage() {
2828
useEffect(() => {
2929
loadPosts();
3030
checkAuth();
31-
// 检查 OAuth 回调
32-
githubService.checkOAuthCallback();
31+
}, []);
32+
33+
// 监听 token 变更(跨 Tab 的 storage 事件 + 同 Tab 自定义事件)以刷新认证状态
34+
useEffect(() => {
35+
const handler = (e) => {
36+
if (!e || e.type === "github_token_updated" || e.key === "github_token") {
37+
checkAuth();
38+
}
39+
};
40+
window.addEventListener("storage", handler);
41+
window.addEventListener("github_token_updated", handler);
42+
return () => {
43+
window.removeEventListener("storage", handler);
44+
window.removeEventListener("github_token_updated", handler);
45+
};
3346
}, []);
3447

3548
const loadPosts = async () => {

src/services/zoneService.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class GitHubService {
1010
setToken(token) {
1111
this.token = token;
1212
localStorage.setItem("github_token", token);
13+
// 通知前端刷新认证状态(同一标签页自定义事件 + 其他标签页 storage 事件)
14+
try {
15+
window.dispatchEvent(new Event("github_token_updated"));
16+
} catch (e) {}
1317
}
1418

1519
// 获取当前用户信息

0 commit comments

Comments
 (0)