forked from trypear/pear-landing-page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
40 lines (35 loc) · 1.22 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
import { NextResponse, type NextRequest } from "next/server";
import { updateSession } from "./utils/supabase/middleware";
import { createClient } from "@/utils/supabase/server";
export async function middleware(request: NextRequest) {
// update the session as before
const response = await updateSession(request);
// Check if the route should be protected
const protectedPaths = ["/api"];
const excludedPaths = ["/api/download", "/api/version-info", "/api/signup"];
const isProtectedRoute = protectedPaths.some((path) =>
request.nextUrl.pathname.startsWith(path),
);
const isExcludedRoute = excludedPaths.some((path) =>
request.nextUrl.pathname.startsWith(path),
);
if (isProtectedRoute && !isExcludedRoute) {
const supabase = createClient();
const {
data: { session },
} = await supabase.auth.refreshSession();
if (!session) {
// redirect to login page if no session
return NextResponse.redirect(new URL("/signin", request.url));
}
}
// if everything fine return response from updateSession
return response;
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
"/signin",
"/signup",
],
};