Added catalog generation, need to bug fix
This commit is contained in:
+28
-15
@@ -1,39 +1,52 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
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",
|
||||
];
|
||||
|
||||
const PUBLIC_ROUTES = ["/", "/login", "/logout", "/about", "/contact"];
|
||||
|
||||
interface State {
|
||||
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: MiddlewareHandlerContext<State>) {
|
||||
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");
|
||||
context.state.isAuthenticated = await isJwtValid(
|
||||
cookies["sessionToken"] ?? "",
|
||||
"NEED TO CHANGE THIS KEY FURTHER IN DEV",
|
||||
);
|
||||
|
||||
return context.next();
|
||||
return await context.next();
|
||||
},
|
||||
async function ensureAuthentication(request: Request, context: MiddlewareHandlerContext<State>) {
|
||||
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"
|
||||
}
|
||||
Location: "/login",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return context.next();
|
||||
}
|
||||
];
|
||||
return await context.next();
|
||||
},
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user