ublo-lib 1.39.9 → 1.39.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../../../../src/esf/components/espace-prive/services/middleware.js"],"names":[],"mappings":"AAEO,kGAwDN;6BA1D4B,aAAa"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
export const middleware = (site, api) => async (request) => {
|
|
3
|
+
const url = request.nextUrl;
|
|
4
|
+
const { pathname } = url;
|
|
5
|
+
const clientID = url.searchParams.get("id") || request.cookies.get("id")?.value;
|
|
6
|
+
const email = url.searchParams.get("email");
|
|
7
|
+
const partnerID = getPartnerID(pathname);
|
|
8
|
+
const connexion = `/espace-prive/${partnerID}/connexion/`;
|
|
9
|
+
const isConnectedToUBLO = request.cookies.get("ublo-editing")?.value === "true";
|
|
10
|
+
if (isConnectedToUBLO) {
|
|
11
|
+
return NextResponse.next();
|
|
12
|
+
}
|
|
13
|
+
if (pathname.startsWith("/espace-prive") &&
|
|
14
|
+
!pathname.endsWith("connexion/")) {
|
|
15
|
+
try {
|
|
16
|
+
const password = request.cookies.get("password")?.value || "no-password";
|
|
17
|
+
const response = await fetch(`${api}/check-auth?site=${site}&password=${password}&partnerID=${partnerID}&id=${clientID}${email ? `&email=${email}` : ""}`);
|
|
18
|
+
const { isLoggedIn, redirect } = await response.json();
|
|
19
|
+
const redirection = redirect || response.status === 401 ? connexion : "/not-found";
|
|
20
|
+
if (isLoggedIn) {
|
|
21
|
+
if (url.searchParams.get("id")) {
|
|
22
|
+
const url = request.nextUrl.clone();
|
|
23
|
+
url.searchParams.delete("id");
|
|
24
|
+
const response = NextResponse.redirect(url);
|
|
25
|
+
response.cookies.set("id", clientID);
|
|
26
|
+
return response;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
NextResponse.next();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
return NextResponse.redirect(new URL(redirection, request.url));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
console.error(error);
|
|
38
|
+
const notFoundUrl = "/not-found";
|
|
39
|
+
return NextResponse.redirect(new URL(notFoundUrl, request.url));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
function getPartnerID(path) {
|
|
44
|
+
const regex = /^\/espace-prive\/([^/]+)\/[^/]*\/?$/;
|
|
45
|
+
const match = path.match(regex);
|
|
46
|
+
if (match) {
|
|
47
|
+
return match[1];
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|