Skip to content

Commit 4ff4f4b

Browse files
committed
added guard to allow localtesting user for the map ingestion page
1 parent 97eb8e1 commit 4ff4f4b

File tree

4 files changed

+60
-40
lines changed

4 files changed

+60
-40
lines changed

pages/maps/ingestion/+guard.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,30 @@
11
import { redirect, render } from "vike/abort";
2+
import { isLocalTesting } from "~/_providers/localTestingAuth";
23

34
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
5+
if (isLocalTesting()) return;
66

7-
const user = pageContext?.user;
7+
const path = pageContext?.urlPathname;
8+
const user = pageContext?.user ?? null;
89
const roles: string[] = Array.isArray(user?.roles)
910
? user.roles
1011
: user?.role
1112
? [user.role]
1213
: [];
14+
const effectiveRoles = roles.length ? roles : ["web_anon"];
15+
const groupNames: string[] = Array.isArray(user?.groups)
16+
? user.groups
17+
.map((g: any) => (typeof g === "string" ? g : g?.name))
18+
.filter(Boolean)
19+
: [];
1320

14-
// pick the correct admin role names for your app:
1521
const allowed =
16-
roles.includes("ingestion_admin") ||
17-
roles.includes("admin") ||
18-
roles.includes("web_admin"); // keep/remove as needed
22+
effectiveRoles.includes("web_anon") ||
23+
effectiveRoles.includes("admin") ||
24+
effectiveRoles.includes("web_admin") ||
25+
groupNames.includes("web_admin");
1926

2027
if (!allowed) {
2128
throw render(403, "Only admins are allowed to access this page.");
22-
// or: throw redirect(`/security/login?return_url=${path}`);
2329
}
2430
}
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-
// }

pages/maps/ingestion/add/+guard.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
import { redirect, render } from "vike/abort";
2-
import { ingestPrefix } from "@macrostrat-web/settings";
2+
import { isLocalTesting } from "~/_providers/localTestingAuth";
33

4-
export const guard = (pageContext) => {
5-
const { user } = pageContext;
4+
export default function guard(pageContext: any) {
5+
if (isLocalTesting()) return;
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.url}`
12-
);
13-
/* The more traditional way, redirect the user:
14-
throw redirect('/login')
15-
*/
16-
}
17-
if (!user.groups.includes(1)) {
18-
// Render the error page and show message to the user
7+
const path = pageContext?.urlPathname;
8+
const user = pageContext?.user ?? null;
9+
const roles: string[] = Array.isArray(user?.roles)
10+
? user.roles
11+
: user?.role
12+
? [user.role]
13+
: [];
14+
const effectiveRoles = roles.length ? roles : ["web_anon"];
15+
const groupNames: string[] = Array.isArray(user?.groups)
16+
? user.groups
17+
.map((g: any) => (typeof g === "string" ? g : g?.name))
18+
.filter(Boolean)
19+
: [];
20+
21+
const allowed =
22+
effectiveRoles.includes("web_anon") ||
23+
effectiveRoles.includes("admin") ||
24+
effectiveRoles.includes("web_admin") ||
25+
groupNames.includes("web_admin");
26+
27+
if (!allowed) {
1928
throw render(403, "Only admins are allowed to access this page.");
2029
}
21-
};
30+
}

src/_providers/auth.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import {
55
} from "@macrostrat/form-components";
66
import h from "@macrostrat/hyper";
77
import { ingestPrefix } from "../../packages/settings";
8+
import { isLocalTesting, mockUser } from "./localTestingAuth";
89

910
async function authTransformer(
1011
action: AuthAction | AsyncAuthAction
1112
): Promise<AuthAction | null> {
1213
/** This transformer is taken directly from Sparrow */
1314
switch (action.type) {
1415
case "get-status":
16+
if (isLocalTesting()) {
17+
return { type: "update-user", user: mockUser };
18+
}
1519
try {
1620
const user = await fetchUser();
1721
return { type: "update-user", user };
@@ -54,6 +58,7 @@ export function AuthProvider(props) {
5458
}
5559

5660
export async function fetchUser() {
61+
if (isLocalTesting()) return mockUser;
5762
const response = await fetch(`${ingestPrefix}/security/me`, {
5863
method: "GET",
5964
credentials: "include",

src/_providers/localTestingAuth.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const isLocalTesting = (): boolean => {
2+
return (
3+
typeof import.meta !== "undefined" &&
4+
(import.meta as any).env?.VITE_LOCAL_TESTING_AUTH === "true"
5+
);
6+
};
7+
8+
export const mockUser = {
9+
id: 46,
10+
name: "Local Tester",
11+
email: "local@test",
12+
role: "web_admin",
13+
roles: ["web_admin"],
14+
groups: [{ id: 1, name: "web_admin" }],
15+
sub: "local-mock",
16+
};

0 commit comments

Comments
 (0)