-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
33 lines (28 loc) · 980 Bytes
/
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
import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export async function middleware(req: NextRequest) {
const res = NextResponse.next()
const supabase = createMiddlewareClient<Database>({ req, res })
const {
data: { user }
} = await supabase.auth.getUser()
if (req.nextUrl.pathname.startsWith('/dashboard')) {
if (!user || user.email !== process.env.NEXT_PUBLIC_ADMIN_EMAIL) {
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/login'
redirectUrl.searchParams.set('redirectUrl', req.nextUrl.pathname)
return NextResponse.redirect(redirectUrl)
}
}
if (req.nextUrl.pathname.startsWith('/api/post')) {
res.headers.set(
'Access-Control-Allow-Origin',
'https://daily-producthunt.dongwook.kim'
)
}
return res
}
export const config = {
matcher: ['/dashboard/:path*', '/api/post']
}