From 8be6c2cdef0ebe4221d512bfdb125a1063c8c130 Mon Sep 17 00:00:00 2001 From: ynwd <10122431+ynwd@users.noreply.github.com> Date: Sun, 8 Sep 2024 09:07:52 +0700 Subject: [PATCH] chore: update session --- modules/auth/mod.tsx | 8 ++++---- modules/blog/mod.ts | 4 +--- modules/docs/mod.ts | 4 +--- modules/index/mod.ts | 5 +---- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/modules/auth/mod.tsx b/modules/auth/mod.tsx index 8b3d9366c..e57a384b7 100644 --- a/modules/auth/mod.tsx +++ b/modules/auth/mod.tsx @@ -87,23 +87,23 @@ async function getUser(accessToken: string) { return data; } -export const callbackHandler = async (req: HttpRequest) => { +export const callbackHandler = async (req: HttpRequest, ctx: Context) => { try { const { response, sessionId, tokens } = await handleCallback( req, ); const user = await getUser(tokens.accessToken); - kv.set([sessionId], user, { expireIn: 60 * 60 * 1000 }); + ctx.server.serverOptions[sessionId] = user; return response; } catch { return new Response(null, { status: STATUS_CODE.InternalServerError }); } }; -export const signoutHandler = async (req: HttpRequest) => { +export const signoutHandler = async (req: HttpRequest, ctx: Context) => { const sessionId = await getSessionId(req); if (!sessionId) throw new Error("session ID is undefined"); - await kv.delete([sessionId]); + ctx.server.serverOptions[sessionId] = undefined; return await signOut(req); }; diff --git a/modules/blog/mod.ts b/modules/blog/mod.ts index fb5defefe..7982b917b 100644 --- a/modules/blog/mod.ts +++ b/modules/blog/mod.ts @@ -2,7 +2,6 @@ import { Fastro } from "@app/mod.ts"; import tocLayout from "@app/modules/toc/toc.layout.tsx"; import tocApp from "@app/modules/toc/toc.page.tsx"; import { getSessionId } from "@app/modules/auth/mod.tsx"; -import { kv } from "@app/utils/db.ts"; export default function (s: Fastro) { s.page("/blog", { @@ -16,8 +15,7 @@ export default function (s: Fastro) { let avatar_url = ""; let html_url = ""; if (sessionId) { - // deno-lint-ignore no-explicit-any - const r = await kv.get([sessionId]) as any; + const r = ctx.server.serverOptions[sessionId]; if (r && r.value) { avatar_url = r.value.avatar_url; html_url = r.value.html_url; diff --git a/modules/docs/mod.ts b/modules/docs/mod.ts index c060601a4..8448b4ab0 100644 --- a/modules/docs/mod.ts +++ b/modules/docs/mod.ts @@ -3,7 +3,6 @@ import tocLayout from "@app/modules/toc/toc.layout.tsx"; import tocApp from "@app/modules/toc/toc.page.tsx"; import { docToc } from "@app/modules/docs/docs.layout.tsx"; import { getSessionId } from "@app/modules/auth/mod.tsx"; -import { kv } from "@app/utils/db.ts"; export default function (s: Fastro) { s.page("/docs", { @@ -17,8 +16,7 @@ export default function (s: Fastro) { let avatar_url = ""; let html_url = ""; if (sessionId) { - // deno-lint-ignore no-explicit-any - const r = await kv.get([sessionId]) as any; + const r = ctx.server.serverOptions[sessionId]; if (r && r.value) { avatar_url = r.value.avatar_url; html_url = r.value.html_url; diff --git a/modules/index/mod.ts b/modules/index/mod.ts index 985329534..eb149f51e 100644 --- a/modules/index/mod.ts +++ b/modules/index/mod.ts @@ -2,7 +2,6 @@ import { Fastro, HttpRequest } from "@app/mod.ts"; import indexApp from "@app/modules/index/index.page.tsx"; import index from "@app/modules/index/index.layout.tsx"; import { getSessionId } from "@app/modules/auth/mod.tsx"; -import { kv } from "@app/utils/db.ts"; function init() { const basePath = Deno.env.get("DENO_DEPLOYMENT_ID") @@ -42,9 +41,7 @@ export default function (s: Fastro) { let avatar_url = ""; let html_url = ""; if (sessionId) { - // deno-lint-ignore no-explicit-any - const r = await kv.get([sessionId]) as any; - console.log("r ==>", r); + const r = ctx.server.serverOptions[sessionId]; if (r && r.value) { avatar_url = r.value.avatar_url; html_url = r.value.html_url;