Added hidden admin only page prop effect
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
import { type RegularTagNode, type TextNode } from "@melvdouc/xml-parser";
|
import { type RegularTagNode, type TextNode } from "@melvdouc/xml-parser";
|
||||||
import { AsyncRoute } from "$fresh/src/server/types.ts";
|
import { AsyncRoute } from "$fresh/src/server/types.ts";
|
||||||
|
|
||||||
interface AuthenticatedState {
|
export interface AuthenticatedState {
|
||||||
isAuthenticated: true;
|
isAuthenticated: true;
|
||||||
session: CasContent;
|
session: CasContent;
|
||||||
|
availablePages: Record<string, string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UnauthenticatedState {
|
interface UnauthenticatedState {
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
"@popov/jwt": "jsr:@popov/jwt@^1.0.1",
|
"@popov/jwt": "jsr:@popov/jwt@^1.0.1",
|
||||||
"@psych/sheet": "jsr:@psych/sheet@^1.0.6",
|
"@psych/sheet": "jsr:@psych/sheet@^1.0.6",
|
||||||
"@std/cli": "jsr:@std/cli@^1.0.10",
|
"@std/cli": "jsr:@std/cli@^1.0.10",
|
||||||
|
"@std/dotenv": "jsr:@std/dotenv@^0.225.3",
|
||||||
"preact": "https://esm.sh/preact@10.22.0",
|
"preact": "https://esm.sh/preact@10.22.0",
|
||||||
"preact/": "https://esm.sh/preact@10.22.0/",
|
"preact/": "https://esm.sh/preact@10.22.0/",
|
||||||
"@preact/signals": "https://esm.sh/*@preact/signals@1.2.2",
|
"@preact/signals": "https://esm.sh/*@preact/signals@1.2.2",
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { defineConfig } from "$fresh/server.ts";
|
import { defineConfig } from "$fresh/server.ts";
|
||||||
import ensureDatabases from "$root/databases/ensure.ts";
|
import ensureDatabases from "$root/databases/ensure.ts";
|
||||||
|
import { load } from "@std/dotenv";
|
||||||
|
|
||||||
|
await load({ envPath: "./.env.development.local", export: true });
|
||||||
await ensureDatabases();
|
await ensureDatabases();
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
server: {
|
server: {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
// This file is automatically updated during development when running `dev.ts`.
|
// This file is automatically updated during development when running `dev.ts`.
|
||||||
|
|
||||||
import * as $_apps_layout from "./routes/(apps)/_layout.tsx";
|
import * as $_apps_layout from "./routes/(apps)/_layout.tsx";
|
||||||
|
import * as $_apps_middleware from "./routes/(apps)/_middleware.ts";
|
||||||
import * as $_apps_mobility_api_insert_mobility from "./routes/(apps)/mobility/api/insert_mobility.ts";
|
import * as $_apps_mobility_api_insert_mobility from "./routes/(apps)/mobility/api/insert_mobility.ts";
|
||||||
import * as $_apps_mobility_index from "./routes/(apps)/mobility/index.tsx";
|
import * as $_apps_mobility_index from "./routes/(apps)/mobility/index.tsx";
|
||||||
import * as $_apps_mobility_partials_admin_edit_mobility from "./routes/(apps)/mobility/partials/(admin)/edit_mobility.tsx";
|
import * as $_apps_mobility_partials_admin_edit_mobility from "./routes/(apps)/mobility/partials/(admin)/edit_mobility.tsx";
|
||||||
@@ -39,6 +40,7 @@ import type { Manifest } from "$fresh/server.ts";
|
|||||||
const manifest = {
|
const manifest = {
|
||||||
routes: {
|
routes: {
|
||||||
"./routes/(apps)/_layout.tsx": $_apps_layout,
|
"./routes/(apps)/_layout.tsx": $_apps_layout,
|
||||||
|
"./routes/(apps)/_middleware.ts": $_apps_middleware,
|
||||||
"./routes/(apps)/mobility/api/insert_mobility.ts":
|
"./routes/(apps)/mobility/api/insert_mobility.ts":
|
||||||
$_apps_mobility_api_insert_mobility,
|
$_apps_mobility_api_insert_mobility,
|
||||||
"./routes/(apps)/mobility/index.tsx": $_apps_mobility_index,
|
"./routes/(apps)/mobility/index.tsx": $_apps_mobility_index,
|
||||||
|
|||||||
@@ -1,22 +1,19 @@
|
|||||||
import { FreshContext } from "$fresh/server.ts";
|
import { FreshContext } from "$fresh/server.ts";
|
||||||
import { Partial } from "$fresh/runtime.ts";
|
import { Partial } from "$fresh/runtime.ts";
|
||||||
import { State } from "$root/defaults/interfaces.ts";
|
import { AuthenticatedState } from "$root/defaults/interfaces.ts";
|
||||||
import { AppProperties } from "$root/defaults/interfaces.ts";
|
|
||||||
import Navbar from "$root/routes/(_islands)/Navbar.tsx";
|
import Navbar from "$root/routes/(_islands)/Navbar.tsx";
|
||||||
|
|
||||||
|
// deno-lint-ignore require-await
|
||||||
export default async function AppLayout(
|
export default async function AppLayout(
|
||||||
request: Request,
|
request: Request,
|
||||||
context: FreshContext<State>,
|
context: FreshContext<AuthenticatedState>,
|
||||||
) {
|
) {
|
||||||
const pathname = new URL(request.url).pathname;
|
const pathname = new URL(request.url).pathname;
|
||||||
const currentApp = pathname.split("/")[1];
|
const currentApp = pathname.split("/")[1];
|
||||||
const properties: AppProperties = (await import(
|
|
||||||
`./${currentApp}/(_props)/props.ts`
|
|
||||||
)).default;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section id="app">
|
<section id="app">
|
||||||
<Navbar currentApp={currentApp} pages={properties.pages} />
|
<Navbar currentApp={currentApp} pages={context.state.availablePages} />
|
||||||
<section id="app-body">
|
<section id="app-body">
|
||||||
<Partial name="body">
|
<Partial name="body">
|
||||||
<context.Component />
|
<context.Component />
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { FreshContext, MiddlewareHandler } from "$fresh/server.ts";
|
||||||
|
import {
|
||||||
|
AppProperties,
|
||||||
|
AuthenticatedState,
|
||||||
|
} from "$root/defaults/interfaces.ts";
|
||||||
|
|
||||||
|
export const handler: MiddlewareHandler<AuthenticatedState>[] = [
|
||||||
|
/**
|
||||||
|
* Check if user is authenticated and add session to context accordingly.
|
||||||
|
* @param request The HTTP incomming request.
|
||||||
|
* @param context The Fresh context object with custom `AuthenticatedState`.
|
||||||
|
* @returns The response from the next middleware.
|
||||||
|
*/
|
||||||
|
async function checkAuthentication(
|
||||||
|
request: Request,
|
||||||
|
context: FreshContext<AuthenticatedState>,
|
||||||
|
): Promise<Response> {
|
||||||
|
const pathname = new URL(request.url).pathname;
|
||||||
|
const currentApp = pathname.split("/")[1];
|
||||||
|
const properties: AppProperties = (await import(
|
||||||
|
`./${currentApp}/(_props)/props.ts`
|
||||||
|
)).default;
|
||||||
|
|
||||||
|
context.state.availablePages = properties.pages;
|
||||||
|
if (
|
||||||
|
context.state.session.eduPersonPrimaryAffiliation == "student" &&
|
||||||
|
Deno.env.get("LOCAL") != "true"
|
||||||
|
) {
|
||||||
|
properties.adminOnly.forEach((page) =>
|
||||||
|
delete context.state.availablePages[page]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await context.next();
|
||||||
|
},
|
||||||
|
];
|
||||||
Reference in New Issue
Block a user