Skip to content

Commit 083bf3c

Browse files
authored
fix: 🧩 修复登录错误 (#51)
1 parent aada46f commit 083bf3c

7 files changed

Lines changed: 68 additions & 53 deletions

File tree

src/app/auth/callback/page.tsx

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ function CallbackContent() {
2121
if (state) {
2222
try {
2323
return decodeURIComponent(state);
24-
} catch (e) {
25-
console.warn('解析state参数失败:', e);
24+
} catch {
25+
// 解析失败时忽略state参数
2626
}
2727
}
2828

@@ -53,7 +53,6 @@ function CallbackContent() {
5353
saveAuthData(authData);
5454
setStatus('登录成功! 正在跳转...');
5555
setState('success');
56-
console.log('认证数据:', authData);
5756

5857
// 跳转到原来的页面
5958
const redirectUrl = getRedirectUrl();
@@ -97,14 +96,27 @@ function CallbackContent() {
9796
const code = searchParams.get('code');
9897

9998
if (!code) {
100-
setStatus('未收到授权码或Token');
99+
setStatus('未收到授权码或Token,请重新尝试登录');
101100
setState('error');
102101

103102
return;
104103
}
105104

105+
setStatus('正在与GitHub服务器通信...');
106+
106107
const { data, error } = await authApi.githubCallback(code, {
107-
onError: (error) => console.error('GitHub认证出错:', error),
108+
onError: (error) => {
109+
// 处理不同类型的错误
110+
if (error instanceof Error) {
111+
if (error.message.includes('超时') || error.message.includes('timeout')) {
112+
setStatus('GitHub认证超时,请重试或检查网络连接');
113+
} else if (error.message.includes('网络')) {
114+
setStatus('网络连接错误,请检查您的网络设置');
115+
} else {
116+
setStatus(`认证失败: ${error.message}`);
117+
}
118+
}
119+
},
108120
});
109121

110122
if (error) {
@@ -122,9 +134,19 @@ function CallbackContent() {
122134
setState('error');
123135
}
124136
} catch (e) {
125-
setStatus('登录过程中发生错误');
137+
if (e instanceof Error) {
138+
if (e.message.includes('Failed to fetch') || e.message.includes('Network')) {
139+
setStatus('网络连接失败,请检查网络设置后重试');
140+
} else if (e.message.includes('timeout') || e.message.includes('超时')) {
141+
setStatus('请求超时,服务器响应较慢,请重试');
142+
} else {
143+
setStatus(`认证失败: ${e.message}`);
144+
}
145+
} else {
146+
setStatus('登录过程中发生未知错误,请重试');
147+
}
148+
126149
setState('error');
127-
console.error(e);
128150
}
129151
};
130152

@@ -155,13 +177,26 @@ function CallbackContent() {
155177
{state === 'error' && (
156178
<div className="flex flex-col items-center">
157179
<AlertCircle className="h-14 w-14 text-red-500 mb-4" />
158-
<p className="text-lg font-medium text-gray-700">{status}</p>
159-
<button
160-
className="mt-6 py-2 px-4 bg-blue-500 hover:bg-blue-600 text-white rounded-lg transition-colors"
161-
onClick={() => router.push('/auth')}
162-
>
163-
返回登录
164-
</button>
180+
<p className="text-lg font-medium text-gray-700 text-center mb-4">{status}</p>
181+
<div className="flex space-x-3">
182+
<button
183+
className="py-2 px-4 bg-blue-500 hover:bg-blue-600 text-white rounded-lg transition-colors"
184+
onClick={() => {
185+
setState('loading');
186+
setStatus('重新尝试认证...');
187+
// 重新触发认证流程
188+
window.location.reload();
189+
}}
190+
>
191+
重试
192+
</button>
193+
<button
194+
className="py-2 px-4 bg-gray-500 hover:bg-gray-600 text-white rounded-lg transition-colors"
195+
onClick={() => router.push('/auth')}
196+
>
197+
返回登录
198+
</button>
199+
</div>
165200
</div>
166201
)}
167202
</div>

src/app/dashboard/more/page.tsx

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import {
99
Mail,
1010
ExternalLink,
1111
} from 'lucide-react';
12-
import { useEffect } from 'react';
13-
14-
import request from '@/services/request';
1512

1613
const moreFeatures = [
1714
{
@@ -74,23 +71,6 @@ const quickLinks = [
7471
];
7572

7673
export default function MorePage() {
77-
useEffect(() => {
78-
async function getMoreFeatures() {
79-
const result = await request.get(
80-
`/api/v1/documents/35/content`,
81-
{ cacheTime: 0 }, // 不缓存,确保数据新鲜
82-
undefined, // mode
83-
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTIzLCJuYW1lIjoiMjA0MjIwNDI4NUBxcS5jb20iLCJlbWFpbCI6IjIwNDIyMDQyODVAcXEuY29tIiwiaWF0IjoxNzUyMDUzMjMwLCJleHAiOjE3NTQ2NDUyMzB9.pz0hnKhcGdq7tDiYxeEnUooLUoPnTg1OvHJtShix78w', // 传递 token,现在 request 已经支持 SSR
84-
);
85-
86-
console.log('result', result);
87-
}
88-
89-
getMoreFeatures();
90-
91-
return () => {};
92-
}, []);
93-
9474
return (
9575
<div className="p-6 max-w-4xl mx-auto">
9676
{/* 页面头部 */}

src/app/docs/_components/DocumentSidebar/SettingsTab.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ const SettingsTab = () => {
154154
)}
155155
onClick={() => {
156156
// TODO: 实现具体的设置操作
157-
console.log(`执行操作: ${item.action}`);
158157
}}
159158
>
160159
<Icon name={item.icon as any} className="h-4 w-4 text-gray-500 dark:text-gray-400" />

src/app/page.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ const Page = () => {
4343
const token = getCookie('auth_token');
4444
setIsLoggedIn(!!token);
4545

46+
// 检查是否是GitHub OAuth回调(如果callback URL配置错误指向了根目录)
47+
const urlParams = new URLSearchParams(window.location.search);
48+
const code = urlParams.get('code');
49+
50+
if (code) {
51+
// 构建完整的callback URL,保留所有参数
52+
const callbackUrl = `/auth/callback${window.location.search}`;
53+
// 使用replace避免在浏览器历史中留下痕迹
54+
window.location.replace(callbackUrl);
55+
56+
return; // 早期返回,避免设置其他监听器
57+
}
58+
4659
const handleMouseMove = (e: MouseEvent) => {
4760
mouseX.set(e.clientX - 192);
4861
mouseY.set(e.clientY - 192);
@@ -238,8 +251,8 @@ const Page = () => {
238251
setTimeout(() => {
239252
setCopiedStates((prev) => ({ ...prev, [key]: false }));
240253
}, 2000);
241-
} catch (err) {
242-
console.error('复制失败', err);
254+
} catch {
255+
// 复制失败时静默处理
243256
}
244257
};
245258

src/components/menus/TextMenu/TextMenu.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function TextMenu({ editor }: { editor: Editor }) {
3333

3434
const handleUpdate = () => {
3535
// 暂时注释掉,避免阻塞TextMenu显示
36-
// console.log('📝 编辑器更新,设置 isTyping = true');
36+
3737
// setIsTyping(true);
3838

3939
// 清除之前的定时器
@@ -43,7 +43,6 @@ export function TextMenu({ editor }: { editor: Editor }) {
4343

4444
// 快速重置输入状态
4545
typingTimeout = window.setTimeout(() => {
46-
console.log('⏰ 重置 isTyping = false');
4746
setIsTyping(false);
4847
}, 100); // 大幅减少延迟时间
4948
};

src/middleware.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,8 @@ export function middleware(request: NextRequest) {
66
// 从cookie获取token
77
const token = request.cookies.get('auth_token')?.value;
88

9-
console.log('===MIDDLEWARE===', {
10-
path: pathname,
11-
hasToken: !!token,
12-
userAgent: request.headers.get('user-agent')?.slice(0, 50),
13-
});
14-
159
// 如果没有token,重定向到登录页
1610
if (!token) {
17-
console.log('No token found, redirecting to /auth');
18-
1911
// 保存原始URL到登录页的查询参数中
2012
const loginUrl = new URL('/auth', request.url);
2113
loginUrl.searchParams.set('redirect_to', encodeURIComponent(pathname + request.nextUrl.search));
@@ -26,9 +18,6 @@ export function middleware(request: NextRequest) {
2618
// 可以在这里添加更多的token验证逻辑
2719
// 比如检查token是否过期等
2820

29-
// Token存在,允许访问
30-
console.log('Token found, allowing access to:', pathname);
31-
3221
return NextResponse.next();
3322
}
3423

src/services/auth/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ export const authApi = {
7171
* @returns 认证结果,包含token等信息
7272
*/
7373
githubCallback: (code: string, errorHandler?: ErrorHandler) =>
74-
request.get<AuthResponse>('/api/v1/auth/callback', {
74+
request.get<AuthResponse>('/api/v1/auth/github/callback', {
7575
params: { code },
7676
errorHandler,
7777
withCredentials: true,
78-
timeout: 30000, // 增加超时到30秒,GitHub OAuth可能需要更长时间
79-
retries: 2, // 允许重试2次
78+
timeout: 60000, // 增加超时到60秒,GitHub OAuth可能需要更长时间
79+
retries: 3, // 允许重试3次
8080
}),
8181

8282
/**

0 commit comments

Comments
 (0)