Partials OK, auto navbar and content

This commit is contained in:
Kevin FEDYNA
2025-01-15 15:43:52 +01:00
parent ed2997d51f
commit 9db588ff02
24 changed files with 510 additions and 321 deletions
+4 -3
View File
@@ -1,11 +1,12 @@
type FooterProps = Record<string | number | symbol, never>;
import { EmptyObject } from "$root/defaults/interfaces.ts";
type FooterProps = EmptyObject;
export default function Footer(_props: FooterProps) {
return (
<footer>
<p>
&copy; 2025 PolyMPR -{" "}
<a href="/about" f-client-nav={false}>About</a>
&copy; 2025 PolyMPR - <a href="/about" f-client-nav={false}>About</a>
</p>
</footer>
);
+13
View File
@@ -0,0 +1,13 @@
interface PartialLinkProps {
link: string;
partial: string;
display: string;
}
export default function PartialLink(props: PartialLinkProps) {
return (
<a href={props.link} f-partial={props.partial}>
{props.display}
</a>
);
}
+1 -6
View File
@@ -1,9 +1,4 @@
export interface AppProperties {
name: string;
icon: string;
pages: Record<string, string>;
adminOnly: string[];
}
import { AppProperties } from "$root/defaults/interfaces.ts";
type AppNavigatorProps = {
apps: Record<string, AppProperties>;
+21 -3
View File
@@ -1,11 +1,29 @@
import PartialLink from "$root/routes/(_components)/PartialLink.tsx";
import { JSX } from "preact/jsx-runtime";
type NavbarProps = {
currentApp: string;
pages: Record<string, string>;
};
export default function Navbar(props: NavbarProps) {
const links: JSX.Element[] = [];
for (const page in props.pages) {
links.push(
<PartialLink
link={`/${props.currentApp}${page === "index" ? "" : `/${page}`}`}
partial={`/${props.currentApp}/partials${
page === "index" ? "" : `/${page}`
}`}
display={props.pages[page]}
/>,
);
}
return (
<>
<p>{JSON.stringify(props.pages)}</p>
</>
<nav>
{links}
</nav>
);
}
+7 -6
View File
@@ -1,24 +1,25 @@
import { FreshContext } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { State } from "$root/routes/_middleware.ts";
import { AppProperties } from "$root/defaults/interfaces.ts";
import Navbar from "$root/routes/(_islands)/Navbar.tsx";
import { AppProperties } from "$root/routes/(_islands)/AppNavigator.tsx";
export default async function AppLayout(
request: Request,
context: FreshContext<State>,
) {
const currentApp = new URL(request.url).pathname;
const pathname = new URL(request.url).pathname;
const currentApp = pathname.split("/")[1];
const properties: AppProperties = (await import(
`./${currentApp}/(_props)/props.ts`
)).default;
)).default;
return (
<>
<Navbar pages={properties.pages} />
<section id="app">
<Navbar currentApp={currentApp} pages={properties.pages} />
<Partial name="body">
<context.Component />
</Partial>
</>
</section>
);
}
+4 -4
View File
@@ -1,4 +1,4 @@
import { AppProperties } from "$root/routes/(_islands)/AppNavigator.tsx";
import { AppProperties } from "$root/defaults/interfaces.ts";
const properties: AppProperties = {
name: "PolyNotes",
@@ -7,9 +7,9 @@ const properties: AppProperties = {
index: "Homepage",
notes: "Notes",
courses: "Courses management",
students: "Students management"
students: "Students management",
},
adminOnly: [ "courses", "students" ]
adminOnly: ["courses", "students"],
};
export default properties;
export default properties;
+2 -9
View File
@@ -1,9 +1,2 @@
type ModulesProps = Record<string | number | symbol, never>;
export default function Modules(_props: ModulesProps) {
return (
<>
<a href="notes/test" f-partial={"notes/partial/test"}>click</a>
</>
);
}
import makeIndex from "$root/defaults/makeIndex.ts";
export default makeIndex(import.meta.dirname!);
@@ -0,0 +1,17 @@
import { Partial } from "$fresh/runtime.ts";
import { RouteConfig } from "$fresh/server.ts";
type ModulesProps = Record<string | number | symbol, never>;
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default function Modules(_props: ModulesProps) {
return (
<Partial name="body">
<a href="notes" f-partial={"notes/partials"}>notes</a>
</Partial>
);
}
@@ -0,0 +1,17 @@
import { Partial } from "$fresh/runtime.ts";
import { RouteConfig } from "$fresh/server.ts";
type ModulesProps = Record<string | number | symbol, never>;
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default function Modules(_props: ModulesProps) {
return (
<Partial name="body">
<a href="notes" f-partial={"notes/partials"}>notes</a>
</Partial>
);
}
+10
View File
@@ -0,0 +1,10 @@
import { getConfig, makePartials } from "$root/defaults/makePartials.tsx";
type NotesIndexProps = Record<string | number | symbol, never>;
export function Index(_props: NotesIndexProps) {
return <a href="notes" f-partial={"notes/partials"}>bip boup</a>;
}
export const config = getConfig();
export default makePartials(Index);
+17
View File
@@ -0,0 +1,17 @@
import { Partial } from "$fresh/runtime.ts";
import { RouteConfig } from "$fresh/server.ts";
type ModulesProps = Record<string | number | symbol, never>;
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default function Modules(_props: ModulesProps) {
return (
<Partial name="body">
<a href="notes" f-partial={"notes/partials"}>notes</a>
</Partial>
);
}
+9 -2
View File
@@ -16,9 +16,16 @@ export default async function App(
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PolyMPR</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,100..800;1,100..800&display=swap" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0&icon_names=school" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,100..800;1,100..800&display=swap"
/>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0&icon_names=school"
/>
<link rel="stylesheet" href="/styles/main.css" />
<link rel="stylesheet" href="/styles/app.css" />
</head>
<body f-client-nav>
<Header link={link} />
+52 -52
View File
@@ -1,52 +1,52 @@
import { FreshContext } from "$fresh/server.ts";
import { getCookies } from "$std/http/cookie.ts";
import { isJwtValid } from "@popov/jwt";
const PUBLIC_ROUTES = [
"/",
"/login",
"/logout",
"/about",
"/partials/about",
"/contact",
];
export interface State {
isAuthenticated: boolean;
}
function isRoutePublic(route: string) {
return PUBLIC_ROUTES.includes(route) || route.match(/\..+$/);
}
export const handler = [
async function checkAuthentication(
request: Request,
context: FreshContext<State>,
) {
const cookies = getCookies(request.headers);
context.state.isAuthenticated = await isJwtValid(
cookies["sessionToken"] ?? "",
"NEED TO CHANGE THIS KEY FURTHER IN DEV",
);
return await context.next();
},
async function ensureAuthentication(
request: Request,
context: FreshContext<State>,
) {
const url = new URL(request.url);
if (!isRoutePublic(url.pathname) && !context.state.isAuthenticated) {
return new Response(null, {
status: 302,
headers: {
Location: "/login",
},
});
}
return await context.next();
},
];
import { FreshContext } from "$fresh/server.ts";
import { getCookies } from "$std/http/cookie.ts";
import { isJwtValid } from "@popov/jwt";
const PUBLIC_ROUTES = [
"/",
"/login",
"/logout",
"/about",
"/partials/about",
"/contact",
];
export interface State {
isAuthenticated: boolean;
}
function isRoutePublic(route: string) {
return PUBLIC_ROUTES.includes(route) || route.match(/\..+$/);
}
export const handler = [
async function checkAuthentication(
request: Request,
context: FreshContext<State>,
) {
const cookies = getCookies(request.headers);
context.state.isAuthenticated = await isJwtValid(
cookies["sessionToken"] ?? "",
"NEED TO CHANGE THIS KEY FURTHER IN DEV",
);
return await context.next();
},
async function ensureAuthentication(
request: Request,
context: FreshContext<State>,
) {
const url = new URL(request.url);
if (!isRoutePublic(url.pathname) && !context.state.isAuthenticated) {
return new Response(null, {
status: 302,
headers: {
Location: "/login",
},
});
}
return await context.next();
},
];
+2 -1
View File
@@ -1,5 +1,6 @@
import { FreshContext, Handlers } from "$fresh/server.ts";
import AppNavigator, { AppProperties } from "$root/routes/(_islands)/AppNavigator.tsx";
import { AppProperties } from "$root/defaults/interfaces.ts";
import AppNavigator from "$root/routes/(_islands)/AppNavigator.tsx";
export const handler: Handlers = {
async GET(_request, context) {
+114 -114
View File
@@ -1,114 +1,114 @@
import { Handlers } from "$fresh/server.ts";
import { State } from "$root/routes/_middleware.ts";
import {
parse,
type RegularTagNode,
type TextNode,
} from "@melvdouc/xml-parser";
import { createJwt } from "@popov/jwt";
import { setCookie } from "$std/http/cookie.ts";
const SERVICE = "https://localhost/login";
const CAS = "https://ident.univ-amu.fr/cas";
interface CasTagNode extends RegularTagNode {
children: [TextNode];
}
interface CasGroupNode extends RegularTagNode {
children: CasTagNode[];
}
interface CasResponse extends RegularTagNode {
children: [TextNode, CasGroupNode];
}
function getTag(tag: CasTagNode): [string, string] {
return [
tag.tagName.replace("cas:", ""),
tag.children[0].value,
];
}
function createUserJWT(casResponse: CasResponse): Promise<string> {
const nodes = casResponse.children[1].children.map(getTag);
const fullUserInfos: Record<string, string | string[]> = {};
nodes.forEach(([key, value]) => {
if (fullUserInfos[key] && Array.isArray(fullUserInfos[key])) {
fullUserInfos[key].push(value);
} else if (fullUserInfos[key]) {
fullUserInfos[key] = [fullUserInfos[key], value];
} else {
fullUserInfos[key] = value;
}
});
const now = Math.floor(Date.now() / 1000);
const oneHour = 60 * 60;
const payload = {
iss: "PolyMPR",
iat: now,
exp: now + oneHour,
aud: "PolyMPR",
user: fullUserInfos,
};
const key = "NEED TO CHANGE THIS KEY FURTHER IN DEV";
return createJwt(payload, key);
}
// deno-lint-ignore no-explicit-any
export const handler: Handlers<any, State> = {
async GET(request, context) {
const url = new URL(request.url);
const ticket = url.searchParams.get("ticket");
if (ticket) {
const response = await fetch(
`${CAS}/serviceValidate?service=${SERVICE}&ticket=${ticket}`,
);
const body = parse(await response.text()) as [RegularTagNode];
const casResponse = body[0].children[0] as CasResponse;
if (casResponse.tagName != "cas:authenticationSuccess") {
return new Response(null, {
status: 302,
headers: {
Location: `${CAS}/login?service=${SERVICE}`,
},
});
}
const headers = new Headers();
setCookie(headers, {
name: "sessionToken",
value: await createUserJWT(casResponse),
});
headers.set("Location", "/apps");
return new Response(null, {
status: 302,
headers,
});
}
if (context.state.isAuthenticated) {
return new Response(null, {
status: 302,
headers: {
Location: "/apps",
},
});
} else {
return new Response(null, {
status: 302,
headers: {
Location: `${CAS}/login?service=${SERVICE}`,
},
});
}
},
};
import { Handlers } from "$fresh/server.ts";
import { State } from "$root/routes/_middleware.ts";
import {
parse,
type RegularTagNode,
type TextNode,
} from "@melvdouc/xml-parser";
import { createJwt } from "@popov/jwt";
import { setCookie } from "$std/http/cookie.ts";
const SERVICE = "https://localhost/login";
const CAS = "https://ident.univ-amu.fr/cas";
interface CasTagNode extends RegularTagNode {
children: [TextNode];
}
interface CasGroupNode extends RegularTagNode {
children: CasTagNode[];
}
interface CasResponse extends RegularTagNode {
children: [TextNode, CasGroupNode];
}
function getTag(tag: CasTagNode): [string, string] {
return [
tag.tagName.replace("cas:", ""),
tag.children[0].value,
];
}
function createUserJWT(casResponse: CasResponse): Promise<string> {
const nodes = casResponse.children[1].children.map(getTag);
const fullUserInfos: Record<string, string | string[]> = {};
nodes.forEach(([key, value]) => {
if (fullUserInfos[key] && Array.isArray(fullUserInfos[key])) {
fullUserInfos[key].push(value);
} else if (fullUserInfos[key]) {
fullUserInfos[key] = [fullUserInfos[key], value];
} else {
fullUserInfos[key] = value;
}
});
const now = Math.floor(Date.now() / 1000);
const oneHour = 60 * 60;
const payload = {
iss: "PolyMPR",
iat: now,
exp: now + oneHour,
aud: "PolyMPR",
user: fullUserInfos,
};
const key = "NEED TO CHANGE THIS KEY FURTHER IN DEV";
return createJwt(payload, key);
}
// deno-lint-ignore no-explicit-any
export const handler: Handlers<any, State> = {
async GET(request, context) {
const url = new URL(request.url);
const ticket = url.searchParams.get("ticket");
if (ticket) {
const response = await fetch(
`${CAS}/serviceValidate?service=${SERVICE}&ticket=${ticket}`,
);
const body = parse(await response.text()) as [RegularTagNode];
const casResponse = body[0].children[0] as CasResponse;
if (casResponse.tagName != "cas:authenticationSuccess") {
return new Response(null, {
status: 302,
headers: {
Location: `${CAS}/login?service=${SERVICE}`,
},
});
}
const headers = new Headers();
setCookie(headers, {
name: "sessionToken",
value: await createUserJWT(casResponse),
});
headers.set("Location", "/apps");
return new Response(null, {
status: 302,
headers,
});
}
if (context.state.isAuthenticated) {
return new Response(null, {
status: 302,
headers: {
Location: "/apps",
},
});
} else {
return new Response(null, {
status: 302,
headers: {
Location: `${CAS}/login?service=${SERVICE}`,
},
});
}
},
};
+30 -30
View File
@@ -1,30 +1,30 @@
import { Handlers } from "$fresh/server.ts";
import { State } from "$root/routes/_middleware.ts";
import { deleteCookie } from "$std/http/cookie.ts";
const SERVICE = "https://localhost/";
const CAS = "https://ident.univ-amu.fr/cas";
// deno-lint-ignore no-explicit-any
export const handler: Handlers<any, State> = {
GET(_request, context) {
if (context.state.isAuthenticated) {
const headers = new Headers();
deleteCookie(headers, "sessionToken", { path: "/" });
headers.set("Location", `${CAS}/logout?service=${SERVICE}`);
return new Response(null, {
status: 302,
headers,
});
} else {
return new Response(null, {
status: 302,
headers: {
Location: "/",
},
});
}
},
};
import { Handlers } from "$fresh/server.ts";
import { State } from "$root/routes/_middleware.ts";
import { deleteCookie } from "$std/http/cookie.ts";
const SERVICE = "https://localhost/";
const CAS = "https://ident.univ-amu.fr/cas";
// deno-lint-ignore no-explicit-any
export const handler: Handlers<any, State> = {
GET(_request, context) {
if (context.state.isAuthenticated) {
const headers = new Headers();
deleteCookie(headers, "sessionToken", { path: "/" });
headers.set("Location", `${CAS}/logout?service=${SERVICE}`);
return new Response(null, {
status: 302,
headers,
});
} else {
return new Response(null, {
status: 302,
headers: {
Location: "/",
},
});
}
},
};