-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
43 lines (41 loc) · 1.35 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { createClient } from '@supabase/supabase-js'
import { authMiddleware, redirectToSignIn } from '@clerk/nextjs'
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL as string,
process.env.SUPABASE_SERVICE_ROLE_KEY as string
)
export default authMiddleware({
afterAuth: async (auth, req, evt) => {
if (!auth.userId && !auth.isPublicRoute) {
return redirectToSignIn({ returnBackUrl: req.url })
}
const lowerCaseLink = req.nextUrl.pathname.slice(1).toLowerCase()
if (lowerCaseLink.length > 0) {
const { data, error } = await supabase
.from('links')
.select('long_link, clicks')
.eq('short_link', lowerCaseLink)
if (error) {
return new NextResponse(JSON.stringify(error), {
status: 500,
})
}
if (data?.length && data[0].long_link) {
await supabase
.from('links')
.update({ clicks: data[0].clicks + 1 })
.eq('short_link', lowerCaseLink)
return NextResponse.redirect(new URL(data[0].long_link))
} else {
return NextResponse.next()
}
}
return NextResponse.next()
},
publicRoutes: ['/((?!account).*)'],
})
export const config = {
matcher: ['/((?!.+.[w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
}