-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
45 lines (37 loc) · 1.07 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
import type { NextRequest } from 'next/server';
import {
ENABLED_OAUTH_PROVIDERS,
getSupabaseAuthSession,
} from '@/lib/services/supabase/auth';
import { NextResponse } from 'next/server';
const publicRoutes = [
'/',
'/auth/callback',
'/sign-in',
'/api/webhooks/stripe',
].concat(
ENABLED_OAUTH_PROVIDERS.map((provider) => `/auth/sign-in/${provider}`),
);
export async function middleware(request: NextRequest) {
const { pathname } = new URL(request.url);
if (publicRoutes.includes(pathname)) {
return NextResponse.next({
request: {
headers: request.headers,
},
});
}
const { authSession, response } = await getSupabaseAuthSession(request);
if (authSession.data.session) {
return response;
}
const nextURL = new URL(request.url);
nextURL.pathname = '/sign-in';
nextURL.searchParams.set('redirect', pathname);
return NextResponse.redirect(nextURL);
}
export const config = {
matcher: [
'/((?!_next/static|_next/image|images|videos|favicon.ico|logo.png|logo.svg|twitter-image.png|opengraph-image.png).*)',
],
};