Skip to content

Commit

Permalink
update: 25/01/14
Browse files Browse the repository at this point in the history
  • Loading branch information
Pleasurecruise committed Jan 14, 2025
1 parent b4d7133 commit d725539
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 38 deletions.
33 changes: 11 additions & 22 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HelmetProvider } from "react-helmet-async";
import {BrowserRouter as Router, Routes, Route, Navigate} from 'react-router-dom';
import {BrowserRouter as Router, Routes, Route} from 'react-router-dom';
import { ThemeProvider } from "@/components/layout/theme-provider.tsx";
import Index from "@/page";
import ErrorPage from "@/page/500";
Expand All @@ -15,20 +15,8 @@ import ToMe from "@/page/interaction/uni-type-tome.tsx";
import I from "@/page/interaction/uni-type-i.tsx";
import OAuth2RedirectHandler from "@/utils/oauthredirecthandler";

import { useAuthStore } from '@/store/tokenStore';
import { useEffect, useState } from 'react';

function App() {
const isAuthenticated = useAuthStore(state => !!state.token);
const [authChecked, setAuthChecked] = useState(false);

useEffect(() => {
setAuthChecked(true);
}, [isAuthenticated]);

if (!authChecked) {
return null;
}
return (
<ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
<HelmetProvider>
Expand All @@ -38,15 +26,16 @@ function App() {
<Route path="/login" element={<Login />} />
<Route path="/oauth2/redirect" element={<OAuth2RedirectHandler />} />
<Route path="/500" element={<ErrorPage />} />
<Route path="/" element={isAuthenticated ? <Dashboard /> : <Navigate to="/login" />} />
<Route path="/account" element={isAuthenticated ? <Account /> : <Navigate to="/login" />} />
<Route path="/mail" element={isAuthenticated ? <MailBox /> : <Navigate to="/login" />} />
<Route path="/monitor" element={isAuthenticated ? <Monitor /> : <Navigate to="/login" />} />
<Route path="/repository" element={isAuthenticated ? <Repository /> : <Navigate to="/login" />} />
<Route path="/star" element={isAuthenticated ? <Star /> : <Navigate to="/login" />} />
<Route path="/project" element={isAuthenticated ? <Project /> : <Navigate to="/login" />} />
<Route path="/me" element={isAuthenticated ? <ToMe /> : <Navigate to="/login" />} />
<Route path="/my" element={isAuthenticated ? <I /> : <Navigate to="/login" />} />

<Route path="/" element={<Dashboard />} />
<Route path="/account" element={<Account />} />
<Route path="/mail" element={<MailBox />} />
<Route path="/monitor" element={<Monitor />} />
<Route path="/repository" element={<Repository />} />
<Route path="/star" element={<Star />} />
<Route path="/project" element={<Project />} />
<Route path="/me" element={<ToMe />} />
<Route path="/my" element={<I />} />
{/* 可以添加更多路由 */}
</Routes>
</Index>
Expand Down
20 changes: 6 additions & 14 deletions web/src/utils/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,22 @@ const api = axios.create({
baseURL: API_BASE_URL,
timeout: 600000
})
const token = useAuthStore.getState().token;
const whiteList: string[] = [
]

// request拦截器
api.interceptors.request.use(
(config) => {
console.log('Sending request:', config); // 添加日志
let isToken = true
whiteList.some((v) => {
if (config.url && config.url == v) {
return isToken = false
}
})
if (token && isToken) {
const token = useAuthStore.getState().token;
if (token) {
config.headers['token'] = 'Bearer ' + token;
} else {
window.location.href = '/login'
return Promise.reject(new Error('No token'))
}
console.log('Sending request:', config);
return config
},
(error: AxiosError) => {
console.log(error) // for debug
console.log(error)
Promise.reject(error)
}
)
Expand All @@ -40,15 +32,15 @@ api.interceptors.response.use(
result => {
const setError = useErrorStore.getState().setError;
if (result.data.code === 1) {
console.log('Response:', result.data); // for debug
console.log('Response:', result.data);
return result.data;
} else {
setError(result.data.msg)
}
return Promise.reject(result.data)
},
err => {
console.log('Error:', err); // for debug
console.log('Error:', err);
const setError = useErrorStore.getState().setError;
if (err.response?.status === 401) {
useAuthStore.getState().logout();
Expand Down
4 changes: 3 additions & 1 deletion web/src/utils/oauthredirecthandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const OAuth2RedirectHandler: React.FC = () => {
console.log('token:', token);
try {
setToken(token);
navigate('/', { replace: true });
setTimeout(() => {
navigate('/', { replace: true });
}, 0);
} catch (error) {
console.log('error:', error);
navigate('/login', { state: { showErrorModal: true, error } });
Expand Down

0 comments on commit d725539

Please sign in to comment.