-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove connect wallet button and improve UX #147
base: main
Are you sure you want to change the base?
Conversation
On first pass I see some bugs if I click different menu items without completing the action on the connect page. My original thought was something simpler at the component level, e.g.
Given we have the middleware though maybe there's a way we can better use it to achieve something similar. It's technically a better/more secure way to do it anyway. Have one untested idea: Update MiddlewareThis would modify import { type NextRequest, NextResponse } from "next/server";
import { updateSession } from "@/utils/supabase/middleware";
// Define routes that require authentication
const protectedPaths = {
// These will return a special header that components can check
'/dashboard': { type: 'component' },
'/dashboard/:path*': { type: 'component' },
// These will still force redirect
'/admin': { type: 'redirect' },
'/admin/:path*': { type: 'redirect' },
// API routes return 401
'/api/protected': { type: 'api' },
'/api/protected/:path*': { type: 'api' }
} as const;
export async function middleware(request: NextRequest) {
const session = await updateSession(request);
const pathname = request.nextUrl.pathname;
// Check if this is a protected path
const matchedPath = Object.entries(protectedPaths).find(([route, _]) => {
const pattern = new RegExp(`^${route.replace(/\/:path\*/, '(/.*)?').replace(/\//g, '\\/')}$`);
return pattern.test(pathname);
});
if (!matchedPath) {
return session; // Not a protected route
}
const [_, config] = matchedPath;
const isAuthenticated = session.headers.get('x-authenticated') === 'true';
if (!isAuthenticated) {
switch (config.type) {
case 'redirect':
// Full redirect for admin routes
const redirectUrl = new URL('/login', request.url);
redirectUrl.searchParams.set('from', pathname);
return NextResponse.redirect(redirectUrl);
case 'api':
// API routes get 401
return NextResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
);
case 'component':
// Add auth status header but allow the route to render
const response = NextResponse.next();
response.headers.set('x-auth-status', 'unauthorized');
return response;
}
}
// User is authenticated, add auth status header
const response = NextResponse.next();
response.headers.set('x-auth-status', 'authorized');
return response;
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
"/dashboard/:path*",
"/admin/:path*",
"/api/protected/:path*",
],
}; I'm not saying that's how we do it but might spark a better idea. I like how we have options for full redirect for something like admin, but can still load mixed content for paths that have some data. Modify LayoutAnother piece to the puzzle may be changing the layouts so the sidebar always renders and the component loaded depends on the auth status, e.g. what happens after the |
Deploying aibtcdev-frontend with Cloudflare Pages
|
I think it aligns with your vision now
|
Flow-wise this looks better when testing it, although the Edit: code structure looks good, seems like a nice simple way to protect routes and the updated auth is a good start. |
|
/connect
page, including the redirect route.useEffect
automatically displays the Xverse wallet popup and allows the user to connect their wallet.