38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
import { getRoute } from "../../contracts/routes.js";
|
|
|
|
/**
|
|
* @param {string} routeId
|
|
* @param {import("../../application/ports/in/application-api.js").SessionState} sessionState
|
|
*/
|
|
export function decideRouteAccess(routeId, sessionState) {
|
|
const route = getRoute(routeId);
|
|
if (route.access === "public") return { allowed: true, action: "none" };
|
|
if (sessionState === "authenticated") {
|
|
return { allowed: true, action: "none" };
|
|
}
|
|
if (sessionState === "recovery-pending") {
|
|
return { allowed: false, action: "wait-for-session" };
|
|
}
|
|
return { allowed: false, action: "show-sign-in" };
|
|
}
|
|
|
|
export function createRedirectLoopGuard() {
|
|
const visitedPairs = new Set();
|
|
|
|
return Object.freeze({
|
|
/**
|
|
* @param {string} source
|
|
* @param {string} target
|
|
*/
|
|
allow(source, target) {
|
|
const pair = `${source}->${target}`;
|
|
if (source === target || visitedPairs.has(pair)) return false;
|
|
visitedPairs.add(pair);
|
|
return true;
|
|
},
|
|
reset() {
|
|
visitedPairs.clear();
|
|
},
|
|
});
|
|
}
|