Skip to content

Commit 783f73b

Browse files
committed
fixing login guard
1 parent ef1a17b commit 783f73b

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

pages/maps/ingestion/+Page.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ function TagFilterManager({ tags, setIngestFilter, ingestFilter }) {
9292
h("h3", ["Filter by tag"]),
9393
h(Tag, {
9494
value: "pending",
95-
active: ingestFilter?.getAll("state").includes("eq.pending"),
95+
active: (ingestFilter?.getAll("state") ?? []).includes("eq.pending"),
9696
onClick: async () => {
9797
updateUrl("state", "eq.pending", setIngestFilter);
9898
},
9999
}),
100100
h(Tag, {
101101
value: "ingested",
102-
active: ingestFilter?.getAll("state").includes("eq.ingested"),
102+
active: (ingestFilter?.getAll("state") ?? []).includes("eq.ingested"),
103103
onClick: async () => {
104104
updateUrl("state", "eq.ingested", setIngestFilter);
105105
},
@@ -108,7 +108,7 @@ function TagFilterManager({ tags, setIngestFilter, ingestFilter }) {
108108
return h(Tag, {
109109
key: tag,
110110
value: tag,
111-
active: ingestFilter?.getAll("tags").includes(`eq.${tag}`),
111+
active: (ingestFilter?.getAll("tags") ?? []).includes(`eq.${tag}`),
112112
onClick: async () => {
113113
updateUrl("tags", `eq.${tag}`, setIngestFilter);
114114
},
@@ -130,19 +130,12 @@ function AddMapButton({ user }) {
130130
);
131131
}
132132

133-
const toggleUrlParam = (
134-
urlSearchParam: URLSearchParams,
135-
key: string,
136-
value: string
137-
) => {
133+
const toggleUrlParam = (urlSearchParam: URLSearchParams | undefined, key: string, value: string) => {
138134
// Check if this key value pair is already in the search params iteratively
139-
if (urlSearchParam.getAll(key).includes(value)) {
140-
urlSearchParam.delete(key, value);
141-
} else {
142-
urlSearchParam.append(key, value);
143-
}
144-
145-
return new URLSearchParams(urlSearchParam.toString());
135+
const sp = urlSearchParam ? new URLSearchParams(urlSearchParam) : new URLSearchParams()
136+
if (sp.getAll(key).includes(value)) sp.delete(key, value)
137+
else sp.append(key, value)
138+
return sp
146139
};
147140

148141
const updateUrl = (

pages/maps/ingestion/+guard.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
import { redirect, render } from "vike/abort";
2-
import { ingestPrefix } from "@macrostrat-web/settings";
32

4-
export const guard = (pageContext) => {
5-
const { user } = pageContext;
3+
export default function guard(pageContext: any) {
4+
const path = pageContext?.urlParsed?.pathname ?? pageContext?.urlPathname;
5+
if (!path?.startsWith("/maps/ingestion/add")) return; // only gate /add
66

7-
// if (user === undefined) {
8-
// // Render the login page while preserving the URL. (This is novel technique
9-
// // which we explain down below.)
10-
// // throw redirect(
11-
// // `${ingestPrefix}/security/login?return_url=${pageContext.urlParsed.pathname}`
12-
// // );
13-
// /* The more traditional way, redirect the user:
14-
// throw redirect('/login')
15-
// */
16-
// return;
17-
// }
18-
// if (!user.groups.includes(1)) {
19-
// // Render the error page and show message to the user
20-
// throw render(403, "Only admins are allowed to access this page.");
21-
// }
22-
};
7+
const user = pageContext?.user;
8+
const roles: string[] = Array.isArray(user?.roles)
9+
? user.roles
10+
: user?.role
11+
? [user.role]
12+
: [];
13+
14+
// pick the correct admin role names for your app:
15+
const allowed =
16+
roles.includes("ingestion_admin") ||
17+
roles.includes("admin") ||
18+
roles.includes("web_admin"); // keep/remove as needed
19+
20+
if (!allowed) {
21+
throw render(403, "Only admins are allowed to access this page.");
22+
// or: throw redirect(`/security/login?return_url=${path}`);
23+
}
24+
}
25+
26+
// if (user === undefined) {
27+
// // Render the login page while preserving the URL. (This is novel technique
28+
// // which we explain down below.)
29+
// // throw redirect(
30+
// // `${ingestPrefix}/security/login?return_url=${pageContext.urlParsed.pathname}`
31+
// // );
32+
// /* The more traditional way, redirect the user:
33+
// throw redirect('/login')
34+
// */
35+
// return;
36+
// }
37+
// if (!user.groups.includes(1)) {
38+
// // Render the error page and show message to the user
39+
// throw render(403, "Only admins are allowed to access this page.");
40+
// }

0 commit comments

Comments
 (0)