From bee8369a6b92e258d84dea7efc4fe06594f2de95 Mon Sep 17 00:00:00 2001 From: William Tso Date: Wed, 23 Apr 2025 18:17:41 +0800 Subject: [PATCH] Remove .env.local file and update middleware, page, and auth callback files to enhance logging and user experience. Translate comments and console logs to English for better clarity. Refactor Create Short URL form and Debug page for improved user feedback and error handling. --- .env.local | 2 - app/auth/callback/route.ts | 30 ++++++------ app/create-shorturl/page.tsx | 90 ++++++++++++++++++------------------ app/debug/page.tsx | 74 +++++++++++++++-------------- app/login/page.tsx | 16 +++---- app/page.tsx | 18 ++++---- lib/auth.tsx | 74 ++++++++++++++--------------- lib/supabase.ts | 28 +++++------ middleware.ts | 56 +++++++++++----------- 9 files changed, 196 insertions(+), 192 deletions(-) delete mode 100644 .env.local diff --git a/.env.local b/.env.local deleted file mode 100644 index 9915ca0..0000000 --- a/.env.local +++ /dev/null @@ -1,2 +0,0 @@ -# Override the port to match what's in .env -PORT=3007 \ No newline at end of file diff --git a/app/auth/callback/route.ts b/app/auth/callback/route.ts index 659bfcd..b682ee0 100644 --- a/app/auth/callback/route.ts +++ b/app/auth/callback/route.ts @@ -9,38 +9,38 @@ export async function GET(request: NextRequest) { console.log('Auth callback received:', { url: request.url, hasCode: !!code }); - // 如果没有code参数,则重定向到登录页面 + // If no code parameter found, redirect to login page if (!code) { - console.log('没有找到code参数,重定向到登录页面'); + console.log('No code parameter found, redirecting to login page'); return NextResponse.redirect(new URL('/login', request.url)); } try { - // 创建supabase客户端 + // Create Supabase client const cookieStore = cookies(); const supabaseRouteHandler = createRouteHandlerClient({ cookies: () => cookieStore }); - // 交换code获取会话 - console.log('开始交换code获取会话'); + // Exchange code for session + console.log('Starting code exchange for session'); const { data, error } = await supabaseRouteHandler.auth.exchangeCodeForSession(code); if (error) { - console.error('交换会话时出错:', error); + console.error('Error exchanging code for session:', error); throw error; } - console.log('成功获取会话,用户:', data.session?.user.email); + console.log('Successfully retrieved session, user:', data.session?.user.email); - // 检查会话是否成功创建 + // Check if session was successfully created if (data.session) { - console.log('会话创建成功:', { + console.log('Session created successfully:', { userId: data.session.user.id, email: data.session.user.email, expiresAt: data.session.expires_at ? new Date(data.session.expires_at * 1000).toISOString() : 'unknown' }); - // 设置额外的cookie以确保客户端能检测到登录状态 - // 使用Next.js的Response来设置cookie + // Set additional cookie to ensure client can detect login status + // Use Next.js Response to set cookie const response = NextResponse.redirect(new URL('/', request.url)); response.cookies.set({ name: 'sb-auth-token', @@ -51,16 +51,16 @@ export async function GET(request: NextRequest) { secure: process.env.NODE_ENV === 'production', httpOnly: false, }); - console.log('设置了备用cookie: sb-auth-token'); + console.log('Set backup cookie: sb-auth-token'); return response; } - // 优先使用应用程序根路径重定向 - console.log('重定向到首页'); + // Redirect to home page by default + console.log('Redirecting to home page'); return NextResponse.redirect(new URL('/', request.url)); } catch (error) { console.error('Auth callback error:', error); - // 出错时重定向到登录页面 + // Redirect to login page on error return NextResponse.redirect( new URL('/login?message=Authentication failed. Please try again.', request.url) ); diff --git a/app/create-shorturl/page.tsx b/app/create-shorturl/page.tsx index 0e0a1c2..3b86aac 100644 --- a/app/create-shorturl/page.tsx +++ b/app/create-shorturl/page.tsx @@ -47,11 +47,11 @@ function CreateShortUrlForm() { const [error, setError] = useState(null); const [success, setSuccess] = useState(false); - // 使用 useEffect 在加载时添加用户信息到表单数据中 + // Use useEffect to add user information to form data when loading useEffect(() => { if (user) { - console.log('当前用户:', user.email); - // 可以在这里添加用户相关数据到表单中 + console.log('Current user:', user.email); + // Can add user-related data to the form here } }, [user]); @@ -93,32 +93,32 @@ function CreateShortUrlForm() { setError(null); try { - // 验证必填字段 + // Validate required fields if (!formData.originalUrl) { - throw new Error('原始 URL 是必填项'); + throw new Error('Original URL is required'); } if (!formData.title) { - throw new Error('标题是必填项'); + throw new Error('Title is required'); } if (!formData.teamId) { - throw new Error('团队是必填项'); + throw new Error('Team is required'); } if (!formData.projectId) { - throw new Error('项目是必填项'); + throw new Error('Project is required'); } if (!formData.domain) { - throw new Error('域名是必填项'); + throw new Error('Domain is required'); } - // 按照API要求构建请求数据 + // Build request data according to API requirements const requestData = { type: "shorturl", attributes: { - // 可以添加任何额外属性,但attributes不能为空 + // Can add any additional attributes, but attributes cannot be empty icon: "" }, shortUrl: { @@ -134,20 +134,20 @@ function CreateShortUrlForm() { tagIds: formData.tags && formData.tags.length > 0 ? formData.tags : undefined }; - // 调用 API 创建 shorturl 资源 + // Call API to create shorturl resource const response = await limqRequest('resource/shorturl', 'POST', requestData as unknown as Record); - console.log('创建成功:', response); + console.log('Creation successful:', response); setSuccess(true); - // 2秒后跳转到链接列表页面 + // Redirect to links list page after 2 seconds setTimeout(() => { router.push('/links'); }, 2000); } catch (err) { - console.error('创建短链接失败:', err); - setError(err instanceof Error ? err.message : '创建短链接失败,请稍后重试'); + console.error('Failed to create short URL:', err); + setError(err instanceof Error ? err.message : 'Failed to create short URL, please try again later'); } finally { setIsSubmitting(false); } @@ -188,7 +188,7 @@ function CreateShortUrlForm() {

- 短链接创建成功!正在跳转... + Short URL created successfully! Redirecting...

@@ -196,10 +196,10 @@ function CreateShortUrlForm() { )}
- {/* 标题 */} + {/* Title */}
- {/* 原始 URL */} + {/* Original URL */}
- {/* 自定义短链接 */} + {/* Custom Short Link */}
@@ -250,14 +250,14 @@ function CreateShortUrlForm() { />

- 留空将生成随机短链接 + Leave blank to generate a random short link

- {/* 域名 */} + {/* Domain */}
- {/* 团队选择 */} + {/* Team Selection */}
({ ...prev, teamId: teamId as string, - // 当团队变更时清除已选项目 + // Clear selected project when team changes projectId: '' })); }} @@ -291,10 +291,10 @@ function CreateShortUrlForm() {
- {/* 项目选择 */} + {/* Project Selection */}
- {/* 描述 */} + {/* Description */}