-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
71 lines (67 loc) · 2.36 KB
/
middleware.ts
File metadata and controls
71 lines (67 loc) · 2.36 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { createMiddlewareClient } from "@supabase/auth-helpers-nextjs";
import { NextRequest, NextResponse } from "next/server";
import { Database } from "./utils/supabase/supabase";
export const revalidate = 0;
export async function middleware(req: NextRequest) {
const res = NextResponse.next(); // Pass request through next-intl first
const supabase = createMiddlewareClient<Database>({ req, res });
await supabase.auth.getSession();
const {
data: { user },
} = await supabase.auth.getUser();
if (!user && !req.nextUrl.pathname.startsWith("/auth")) {
return NextResponse.redirect(new URL("/auth/signIn", req.url));
}
if (
user &&
req.nextUrl.pathname.startsWith("/auth") &&
req.nextUrl.pathname !== "/auth/passwordReset/update"
) {
return NextResponse.redirect(new URL("/home", req.url));
}
if (user && req.nextUrl.pathname === "/constructors/newAccount") {
const { data: personal_info, error: personal_info_error } = await supabase
.from("personal_info")
.select("is_first_initialised")
.eq("id", user.id as string);
if (personal_info_error) {
return NextResponse.redirect(new URL("/home", req.url));
} else if (!personal_info[0].is_first_initialised) {
return res;
} else {
return NextResponse.redirect(new URL("/home", req.url));
}
}
if (user && req.nextUrl.pathname === "/constructors/finishAccount") {
const { data: profile, error: profile_error } = await supabase
.from("profiles")
.select("is_first_initialised")
.eq("id", user.id as string);
if (profile_error) {
return NextResponse.redirect(new URL("/home", req.url));
} else if (!profile[0]?.is_first_initialised) {
return res;
} else {
return NextResponse.redirect(new URL("/home", req.url));
}
}
if (user && req.nextUrl.pathname === "/") {
return NextResponse.redirect(new URL("/home", req.url));
}
if (user && req.nextUrl.pathname === "/auth/passwordReset/update") {
const { data } = await supabase
.from("personal_info")
.select("is_password_gonna_reset")
.eq("id", user.id)
.single();
if (data?.is_password_gonna_reset) {
return res;
} else {
return NextResponse.redirect(new URL("/home", req.url));
}
}
return res;
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};