Added hidden admin only page prop effect
This commit is contained in:
@@ -1,22 +1,19 @@
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
import { Partial } from "$fresh/runtime.ts";
|
||||
import { State } from "$root/defaults/interfaces.ts";
|
||||
import { AppProperties } from "$root/defaults/interfaces.ts";
|
||||
import { AuthenticatedState } from "$root/defaults/interfaces.ts";
|
||||
import Navbar from "$root/routes/(_islands)/Navbar.tsx";
|
||||
|
||||
// deno-lint-ignore require-await
|
||||
export default async function AppLayout(
|
||||
request: Request,
|
||||
context: FreshContext<State>,
|
||||
context: FreshContext<AuthenticatedState>,
|
||||
) {
|
||||
const pathname = new URL(request.url).pathname;
|
||||
const currentApp = pathname.split("/")[1];
|
||||
const properties: AppProperties = (await import(
|
||||
`./${currentApp}/(_props)/props.ts`
|
||||
)).default;
|
||||
|
||||
return (
|
||||
<section id="app">
|
||||
<Navbar currentApp={currentApp} pages={properties.pages} />
|
||||
<Navbar currentApp={currentApp} pages={context.state.availablePages} />
|
||||
<section id="app-body">
|
||||
<Partial name="body">
|
||||
<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