forked from omnistrate-oss/saasbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
100 lines (83 loc) · 2.92 KB
/
middleware.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { jwtDecode } from "jwt-decode";
import { NextResponse } from "next/server";
import { baseURL } from "src/axios";
import { getEnvironmentType } from "src/server/utils/getEnvironmentType";
const environmentType = getEnvironmentType();
export async function middleware(request) {
const authToken = request.cookies.get("token");
const path = request.nextUrl.pathname;
if (
path.startsWith("/signup") ||
path.startsWith("/reset-password") ||
path.startsWith("/change-password")
) {
if (environmentType === "PROD") return;
}
const redirectToSignIn = () => {
const path = request.nextUrl.pathname;
// Prevent Redirecting to the Same Page
if (path.startsWith("/signin")) return;
const redirectPath = "/signin";
const response = NextResponse.redirect(new URL(redirectPath, request.url));
response.headers.set(`x-middleware-cache`, `no-cache`);
return response;
};
if (!authToken?.value || jwtDecode(authToken.value).exp < Date.now() / 1000) {
return redirectToSignIn();
}
try {
const userData = await fetch(`${baseURL}/user`, {
method: "GET",
headers: {
Authorization: `Bearer ${authToken.value}`,
},
});
if (userData?.status !== 200) {
return redirectToSignIn();
}
// Subscriptions page should only be accessible in PROD
// Removing This for Now
// if (request.nextUrl.pathname.startsWith("/subscriptions")) {
// if (environmentType !== "PROD") {
// const response = NextResponse.redirect(
// new URL("/service-plans", request.url)
// );
// response.headers.set(`x-middleware-cache`, `no-cache`);
// return response;
// }
// }
if (request.nextUrl.pathname.startsWith("/signin")) {
let destination = request.nextUrl.searchParams.get("destination");
destination =
destination &&
(destination.startsWith("%2Fservice-plans") ||
destination.startsWith("/service-plans"))
? decodeURIComponent(destination)
: "/service-plans";
const response = NextResponse.redirect(new URL(destination, request.url));
response.headers.set(`x-middleware-cache`, `no-cache`);
return response;
}
} catch (error) {
console.log("Middleware Error", error?.response?.data);
redirectToSignIn();
}
const response = NextResponse.next();
response.headers.set(`x-middleware-cache`, `no-cache`);
return response;
}
/*
* Match all request paths except for the ones starting with:
* - signup
* - reset-password
* - change-password
* - validate-token
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
export const config = {
matcher: [
"/((?!api/action|api/signup|api/signin|api/reset-password|api/provider-details|idp-auth|api/sign-in-with-idp|privacy-policy|terms-of-use|favicon.ico|_next/image|_next/static|static|validate-token).*)",
],
};