Enhance authentication flow by implementing ProtectedRoute component across various pages, ensuring users are redirected based on their authentication status. Update login page to support Google sign-in and handle redirect URLs after login. Modify analytics and links pages to include loading indicators and protected access. Update next.config.ts to enable middleware for edge functions.

This commit is contained in:
2025-04-23 17:36:54 +08:00
parent c56410b4de
commit 1b4e0bafc7
15 changed files with 597 additions and 85 deletions

View File

@@ -0,0 +1,32 @@
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs';
import { cookies } from 'next/headers';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export async function GET(request: NextRequest) {
const requestUrl = new URL(request.url);
const code = requestUrl.searchParams.get('code');
// 如果没有code参数则重定向到登录页面
if (!code) {
return NextResponse.redirect(new URL('/login', request.url));
}
try {
// 创建supabase客户端
const cookieStore = cookies();
const supabaseRouteHandler = createRouteHandlerClient({ cookies: () => cookieStore });
// 交换code获取会话
await supabaseRouteHandler.auth.exchangeCodeForSession(code);
// 直接重定向到首页,避免中间跳转
return NextResponse.redirect(new URL('/', request.url));
} catch (error) {
console.error('Auth callback error:', error);
// 出错时重定向到登录页面
return NextResponse.redirect(
new URL('/login?message=Authentication failed. Please try again.', request.url)
);
}
}