-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
48 lines (44 loc) · 1.35 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
46
47
48
import { createMiddlewareSupabaseClient } from "@supabase/auth-helpers-nextjs";
import { NextResponse, NextRequest } from "next/server";
export async function middleware(req: NextRequest) {
// Create a response to handoff to supabase client to modify response headers
const res = NextResponse.next();
const isAuthPage =
req.nextUrl.pathname.includes("/login") ||
req.nextUrl.pathname.includes("/register");
const isCheckoutPage =
req.nextUrl.pathname.includes("/manage-subscription") ||
req.nextUrl.pathname.includes("/profile") ||
req.nextUrl.pathname.includes("/dashboard");
// Create authenticated supabase client
const supabase = createMiddlewareSupabaseClient({ req, res });
const {
data: { session },
} = await supabase.auth.getSession();
if (isAuthPage) {
if (!session?.user) {
return res;
}
}
if (isCheckoutPage && session?.user) {
return res;
}
const redirectUrl = req.nextUrl.clone();
redirectUrl.pathname = "/";
// redirectUrl.searchParams.set(
// `redirectedFrom`,
// encodeURIComponent(req.nextUrl.pathname)
// );
return NextResponse.redirect(redirectUrl);
}
export const config = {
matcher: [
"/auth/:path*",
"/settings/:path*",
"/auth/login",
"/auth/register",
"/settings/manage-subscription",
"/user/:path*/profile",
"/user/:path*/dashboard",
],
};