forked from ZuzaluSoftwareBuilder/ZuzaluCityOS
-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.ts
46 lines (36 loc) · 1.11 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { supabase } from '@/utils/supabase/client';
export async function middleware(request: NextRequest) {
const { pathname, origin } = request.nextUrl;
if (pathname.startsWith('/s/') || pathname.startsWith('/e/')) {
let parts = pathname.split('/').filter(Boolean);
let name = '';
let hash = '';
if (parts.length >= 2) {
name = parts[1];
const hasHash = parts[2]?.length === 1;
hash = !hasHash ? '0' : parts[2];
parts = parts.slice(hasHash ? 3 : 2);
} else {
return NextResponse.redirect(origin);
}
const { data, error } = await supabase
.from('url')
.select('*')
.eq('name', name)
.eq('hash', hash)
.single();
if (error) {
return NextResponse.redirect(origin);
}
const { ceramicId, type } = data;
return NextResponse.redirect(
`${origin}/${type}/${ceramicId}${parts.length ? `/${parts.join('/')}` : ''}`,
);
}
return NextResponse.next();
}
export const config = {
matcher: ['/s/:path*', '/e/:path*'],
};