-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.ts
33 lines (24 loc) · 1.03 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
import { NextResponse, type NextRequest } from "next/server";
import { createClient } from "@/utils/supabase/middleware";
export async function middleware(req: NextRequest) {
const { supabase, response } = createClient(req);
const { data } = await supabase.auth.getSession();
//If the user is accessing a page other than dashboard, do nothing
if (req.nextUrl.pathname !== "/dashboard") return response;
const userLoggedIn = !!data?.session?.user;
// If user is not logged in, redirect to login page
if (!userLoggedIn) {
return NextResponse.redirect(req.nextUrl.origin + "/login");
}
// If user is logged in.
//Fetch the user's waitlist entry
const { data: waitlistEntry } = await supabase
.from("waitlist")
.select("approved")
.eq("user_id", data.session?.user.id)
.single();
// The user is approved, allow access to dashboard
if (waitlistEntry?.approved) return response;
// The user is not approved, redirect to waitlist page
return NextResponse.redirect(req.nextUrl.origin + "/waitlist");
}