|
1 | 1 | import { Container, Flex, Text, HStack, VStack, Box } from "@chakra-ui/react"; |
2 | 2 | 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"; |
5 | 5 | import { motion } from "framer-motion"; |
6 | 6 | import "./index.css"; |
7 | 7 | import { ProfileImage } from "./components/ProfileImage"; |
@@ -110,6 +110,32 @@ function LoadingScreen() { |
110 | 110 | } |
111 | 111 |
|
112 | 112 | 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 | + |
113 | 139 | return ( |
114 | 140 | <Box minH="100vh" bg="#8FC4FF" position="relative" zIndex={1}> |
115 | 141 | <Suspense fallback={<LoadingScreen />}> |
|
0 commit comments