-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
26 lines (21 loc) · 791 Bytes
/
middleware.ts
File metadata and controls
26 lines (21 loc) · 791 Bytes
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
import { createMiddlewareClient } from "@supabase/auth-helpers-nextjs";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { Database } from "./types/supabase";
const protectedPaths = ["/account", "/list"];
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
const supabase = createMiddlewareClient<Database>({ req, res });
// Calling this also makes sure that the session stays refreshed
const {
data: { user },
} = await supabase.auth.getUser();
// Redirect to login when trying to access protected pages
if (
!user &&
protectedPaths.some((path) => req.nextUrl.pathname.startsWith(path))
) {
return NextResponse.redirect(new URL("/login", req.url));
}
return res;
}