Skip to content

Commit

Permalink
redirect .lens usernames to profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
kualta committed Jul 7, 2024
1 parent acf22ed commit 14774dc
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ import { getCookieAuth } from "./utils/getCookieAuth";
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;

// Check for the namespace
const namespace = /^\/u\/lens\/(.+)$/;
const match = pathname.match(namespace);
// Check for the .lens postfix
const lensNamespace = /^\/u\/(.+)\.lens$/;
const postfixMatch = pathname.match(lensNamespace);
if (postfixMatch) {
const username = postfixMatch[1];
return NextResponse.redirect(new URL(`/u/${username}`, request.url));
}

if (match) {
const username = match[1];
// Redirect to profile page
// Check for the lens namespace
const oldLensNamespace = /^\/u\/lens\/(.+)$/;
const namespaceMatch = pathname.match(oldLensNamespace);
if (namespaceMatch) {
const username = namespaceMatch[1];
return NextResponse.redirect(new URL(`/u/${username}`, request.url));
}

// Only run this middleware for the base URL
if (pathname === "/") {
const { isAuthenticated } = getCookieAuth();

if (isAuthenticated) {
// If authenticated, redirect to /home
return NextResponse.redirect(new URL("/home", request.url));
Expand Down

0 comments on commit 14774dc

Please sign in to comment.