Skip to content

Commit

Permalink
chore: update session
Browse files Browse the repository at this point in the history
  • Loading branch information
ynwd committed Sep 8, 2024
1 parent a7cb824 commit 8be6c2c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
8 changes: 4 additions & 4 deletions modules/auth/mod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
4 changes: 1 addition & 3 deletions modules/blog/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions modules/docs/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions modules/index/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8be6c2c

Please sign in to comment.