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.
This commit is contained in:
@@ -2,76 +2,76 @@ import { NextResponse } from 'next/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
export function middleware(request: NextRequest) {
|
||||
// 获取请求的路径
|
||||
// Get the request path
|
||||
const path = request.nextUrl.pathname;
|
||||
console.log(`[Middleware] 请求路径: ${path}`);
|
||||
console.log(`[Middleware] Request path: ${path}`);
|
||||
|
||||
// 定义不需要验证的路径
|
||||
// Define paths that don't require authentication
|
||||
const publicPaths = ['/login', '/register', '/auth/callback'];
|
||||
|
||||
// API 路由不需要验证
|
||||
// API routes don't require authentication
|
||||
if (path.startsWith('/api/')) {
|
||||
console.log('[Middleware] API路由,跳过验证');
|
||||
console.log('[Middleware] API route, skipping validation');
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// 静态资源不需要验证
|
||||
// Static resources don't require authentication
|
||||
if (path.includes('/_next/') || path.includes('/static/') || path.match(/\.(ico|png|jpg|jpeg|svg|css|js)$/)) {
|
||||
console.log('[Middleware] 静态资源,跳过验证');
|
||||
console.log('[Middleware] Static resource, skipping validation');
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// 检查是否是公开路径
|
||||
// Check if it's a public path
|
||||
const isPublicPath = publicPaths.some(publicPath => path === publicPath || path.startsWith(publicPath));
|
||||
console.log(`[Middleware] 是公开路径: ${isPublicPath}`);
|
||||
console.log(`[Middleware] Is public path: ${isPublicPath}`);
|
||||
|
||||
// 获取所有 cookie
|
||||
// Get all cookies
|
||||
const allCookies = Object.fromEntries(request.cookies.getAll().map(c => [c.name, c.value]));
|
||||
console.log('[Middleware] 所有Cookie:', JSON.stringify(allCookies));
|
||||
console.log('[Middleware] All cookies:', JSON.stringify(allCookies));
|
||||
|
||||
// 检查各个认证cookie
|
||||
// Check each authentication cookie
|
||||
const accessToken = request.cookies.get('sb-access-token');
|
||||
const refreshToken = request.cookies.get('sb-refresh-token');
|
||||
const providerToken = request.cookies.get('sb-provider-token');
|
||||
const authToken = request.cookies.get('supabase-auth-token');
|
||||
const customAuthToken = request.cookies.get('sb-auth-token');
|
||||
|
||||
console.log('[Middleware] 认证Cookie详情:', {
|
||||
'sb-access-token': accessToken ? '存在' : '不存在',
|
||||
'sb-refresh-token': refreshToken ? '存在' : '不存在',
|
||||
'sb-provider-token': providerToken ? '存在' : '不存在',
|
||||
'supabase-auth-token': authToken ? '存在' : '不存在',
|
||||
'sb-auth-token': customAuthToken ? '存在' : '不存在'
|
||||
console.log('[Middleware] Auth cookie details:', {
|
||||
'sb-access-token': accessToken ? 'exists' : 'not found',
|
||||
'sb-refresh-token': refreshToken ? 'exists' : 'not found',
|
||||
'sb-provider-token': providerToken ? 'exists' : 'not found',
|
||||
'supabase-auth-token': authToken ? 'exists' : 'not found',
|
||||
'sb-auth-token': customAuthToken ? 'exists' : 'not found'
|
||||
});
|
||||
|
||||
// 检查用户是否登录
|
||||
// Check if user is logged in
|
||||
const isLoggedIn = !!(accessToken || refreshToken || providerToken || authToken || customAuthToken);
|
||||
console.log(`[Middleware] 用户是否登录: ${isLoggedIn}`);
|
||||
console.log(`[Middleware] User is logged in: ${isLoggedIn}`);
|
||||
|
||||
// 如果是公开路径但已登录,重定向到首页
|
||||
// If it's a public path but user is logged in, redirect to home page
|
||||
if (isPublicPath && isLoggedIn) {
|
||||
console.log('[Middleware] 已登录用户访问公开路径,重定向到首页');
|
||||
console.log('[Middleware] User is logged in and accessing public path, redirecting to home page');
|
||||
return NextResponse.redirect(new URL('/', request.url));
|
||||
}
|
||||
|
||||
// 如果不是公开路径且未登录,重定向到登录页
|
||||
// If it's not a public path and user is not logged in, redirect to login page
|
||||
if (!isPublicPath && !isLoggedIn) {
|
||||
console.log('[Middleware] 未登录用户访问私有路径,重定向到登录页');
|
||||
console.log('[Middleware] User is not logged in and accessing private path, redirecting to login page');
|
||||
const redirectUrl = new URL('/login', request.url);
|
||||
redirectUrl.searchParams.set('redirect', encodeURIComponent(request.url));
|
||||
return NextResponse.redirect(redirectUrl);
|
||||
}
|
||||
|
||||
console.log('[Middleware] 通过验证,允许访问');
|
||||
console.log('[Middleware] Validation passed, allowing access');
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// 配置中间件匹配的路径
|
||||
// Configure middleware matching paths
|
||||
export const config = {
|
||||
matcher: [
|
||||
// 匹配所有路径,但排除静态资源
|
||||
// Match all paths, but exclude static resources
|
||||
'/((?!_next/static|_next/image|favicon.ico).*)',
|
||||
// 明确包括重要的路由
|
||||
// Explicitly include important routes
|
||||
'/',
|
||||
'/analytics',
|
||||
'/links',
|
||||
|
||||
Reference in New Issue
Block a user