You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.5 KiB
56 lines
1.5 KiB
import { FRONTEND_PAGE_ALLOWLIST, FRONTEND_PAGE_GUEST_ONLY } from "common/config"
|
|
import { normalizePath, pathToRegexp } from "common/utils/path"
|
|
|
|
const PUBLIC_ROUTE_PREFIXES: string[] = ['/articles/', '/projects/'];
|
|
|
|
export const DEFAULT_AUTHENTICATED_LANDING_PATH = "/";
|
|
|
|
function matchesExactOrPrefix(path: string, exact: Set<string>, prefixes: string[]) {
|
|
const normalized = normalizePath(path);
|
|
if (exact.has(normalized)) {
|
|
return true;
|
|
}
|
|
if (prefixes.some((prefix) => normalized.startsWith(prefix))) {
|
|
return true;
|
|
}
|
|
return Array.from(exact).some((rule) => {
|
|
if (!rule.includes(":")) {
|
|
return false;
|
|
}
|
|
const regex = pathToRegexp(rule);
|
|
return regex.test(normalized);
|
|
});
|
|
}
|
|
|
|
export function isPublicRoute(path: string) {
|
|
return matchesExactOrPrefix(path, FRONTEND_PAGE_ALLOWLIST, PUBLIC_ROUTE_PREFIXES);
|
|
}
|
|
|
|
export function isGuestOnlyRoute(path: string) {
|
|
return FRONTEND_PAGE_GUEST_ONLY.has(normalizePath(path));
|
|
}
|
|
|
|
export function normalizeSafeRedirect(
|
|
value: unknown,
|
|
fallback = DEFAULT_AUTHENTICATED_LANDING_PATH,
|
|
) {
|
|
if (typeof value !== "string") {
|
|
return fallback;
|
|
}
|
|
|
|
const candidate = value.trim();
|
|
if (!candidate || !candidate.startsWith("/") || candidate.startsWith("//")) {
|
|
return fallback;
|
|
}
|
|
|
|
const lower = candidate.toLowerCase();
|
|
if (
|
|
lower.startsWith("/http:") ||
|
|
lower.startsWith("/https:") ||
|
|
lower.startsWith("/javascript:")
|
|
) {
|
|
return fallback;
|
|
}
|
|
|
|
return candidate;
|
|
}
|
|
|