Skip to content

Commit

Permalink
refactor: cleanup middleware (#333)
Browse files Browse the repository at this point in the history
* refactor: cleanup middleware

* revert
  • Loading branch information
iuioiua authored Jul 3, 2023
1 parent 516d745 commit 6918b14
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function Header(
<CircleFilled class="absolute top-0.5 right-0.5 text-pink-700 w-2 h-2" />
)}
</a>
<a href="Submit" class={BUTTON_STYLES}>Submit</a>
<a href="/submit" class={BUTTON_STYLES}>Submit</a>
</nav>
</header>
);
Expand Down
51 changes: 25 additions & 26 deletions routes/_middleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import { MiddlewareHandlerContext } from "$fresh/server.ts";
import { walk } from "std/fs/walk.ts";
import { getSessionId } from "kv_oauth";
import { redirect, setRedirectUrlCookie } from "@/utils/redirect.ts";
import { redirect } from "@/utils/redirect.ts";
import { Status } from "std/http/http_status.ts";
import { getUserBySession, ifUserHasNotifications } from "@/utils/db.ts";
import { incrVisitsCountByDay } from "@/utils/db.ts";
Expand All @@ -13,28 +12,18 @@ export interface State extends MetaProps {
hasNotifications?: boolean;
}

const STATIC_DIR_ROOT = new URL("../static", import.meta.url);
const staticFileNames: string[] = [];
for await (const { name } of walk(STATIC_DIR_ROOT, { includeDirs: false })) {
staticFileNames.push(name);
}

export async function handler(
async function redirectToNewOrigin(
req: Request,
ctx: MiddlewareHandlerContext<State>,
ctx: MiddlewareHandlerContext,
) {
const { pathname, hostname } = new URL(req.url);

if (hostname === "saaskit.deno.dev") {
return redirect("https://hunt.deno.land", Status.Found);
}
const { hostname } = new URL(req.url);
return hostname === "saaskit.deno.dev"
? redirect("https://hunt.deno.land", Status.Found)
: await ctx.next();
}

// Don't process session-related data for keepalive and static requests
if (["_frsh", ...staticFileNames].some((part) => pathname.includes(part))) {
return await ctx.next();
}

await incrVisitsCountByDay(new Date());
async function setState(req: Request, ctx: MiddlewareHandlerContext<State>) {
if (ctx.destination !== "route") return await ctx.next();

const sessionId = await getSessionId(req);
ctx.state.sessionId = sessionId;
Expand All @@ -44,11 +33,21 @@ export async function handler(
ctx.state.hasNotifications = await ifUserHasNotifications(user!.id);
}

const res = await ctx.next();
return await ctx.next();
}

if (ctx.destination === "route" && pathname === "/signin") {
setRedirectUrlCookie(req, res);
}
async function recordVisit(
_req: Request,
ctx: MiddlewareHandlerContext,
) {
if (ctx.destination !== "route") return await ctx.next();

return res;
await incrVisitsCountByDay(new Date());
return await ctx.next();
}

export const handler = [
redirectToNewOrigin,
setState,
recordVisit,
];
10 changes: 6 additions & 4 deletions routes/signin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import type { Handlers } from "$fresh/server.ts";
import type { State } from "./_middleware.ts";
import { redirect } from "@/utils/redirect.ts";
import { redirect, setRedirectUrlCookie } from "@/utils/redirect.ts";
import { signIn } from "kv_oauth";
import { oauth2Client } from "@/utils/oauth2_client.ts";

Expand All @@ -12,8 +12,10 @@ export const handler: Handlers<any, State> = {
* If not logged in, it continues to rendering the login page.
*/
async GET(req, ctx) {
return ctx.state.sessionId
? redirect("/")
: await signIn(req, oauth2Client);
if (ctx.state.sessionId !== undefined) return redirect("/");

const resp = await signIn(req, oauth2Client);
setRedirectUrlCookie(req, resp);
return resp;
},
};

0 comments on commit 6918b14

Please sign in to comment.