From 96d4f5cb0c23bfc6f776493f260c99cc44808057 Mon Sep 17 00:00:00 2001 From: fdemir Date: Sun, 14 Apr 2024 01:39:34 +0300 Subject: [PATCH 1/3] refactor: kv_oauth --- deno.json | 1 - plugins/kv_oauth.ts | 15 ++++++--------- plugins/session.ts | 20 +++++++------------- utils/github.ts | 4 ++-- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/deno.json b/deno.json index ecd0a97cd..9c8e35ef2 100644 --- a/deno.json +++ b/deno.json @@ -31,7 +31,6 @@ "tailwindcss/plugin": "npm:/tailwindcss@3.4.1/plugin.js", "$std/": "https://deno.land/std@0.208.0/", "stripe": "npm:/stripe@13.5.0", - "kv_oauth/": "https://deno.land/x/deno_kv_oauth@v0.9.1/", "tabler_icons_tsx/": "https://deno.land/x/tabler_icons_tsx@0.0.4/tsx/", "fresh_charts/": "https://deno.land/x/fresh_charts@0.3.1/" }, diff --git a/plugins/kv_oauth.ts b/plugins/kv_oauth.ts index 8a741f292..822833e48 100644 --- a/plugins/kv_oauth.ts +++ b/plugins/kv_oauth.ts @@ -1,11 +1,6 @@ // Copyright 2023-2024 the Deno authors. All rights reserved. MIT license. import type { Plugin } from "$fresh/server.ts"; -import { - createGitHubOAuthConfig, - handleCallback, - signIn, - signOut, -} from "kv_oauth/mod.ts"; +import { createGitHubOAuthConfig, createHelpers } from "jsr:@deno/kv-oauth"; import { createUser, getUser, @@ -15,6 +10,9 @@ import { import { isStripeEnabled, stripe } from "@/utils/stripe.ts"; import { getGitHubUser } from "@/utils/github.ts"; +export const { signIn, handleCallback, signOut, getSessionId } = createHelpers( + createGitHubOAuthConfig() +); // Exported for mocking and spying in e2e tests export const _internals = { handleCallback }; @@ -31,14 +29,13 @@ export default { routes: [ { path: "/signin", - handler: async (req) => await signIn(req, createGitHubOAuthConfig()), + handler: async (req) => await signIn(req), }, { path: "/callback", handler: async (req) => { const { response, tokens, sessionId } = await _internals.handleCallback( - req, - createGitHubOAuthConfig(), + req ); const githubUser = await getGitHubUser(tokens.accessToken); diff --git a/plugins/session.ts b/plugins/session.ts index cc12fd7fa..c204c6138 100644 --- a/plugins/session.ts +++ b/plugins/session.ts @@ -1,10 +1,10 @@ // Copyright 2023-2024 the Deno authors. All rights reserved. MIT license. import { Plugin } from "$fresh/server.ts"; import type { FreshContext } from "$fresh/server.ts"; -import { getSessionId } from "kv_oauth/mod.ts"; import { getUserBySession } from "@/utils/db.ts"; import type { User } from "@/utils/db.ts"; import { UnauthorizedError } from "@/utils/http.ts"; +import { getSessionId } from "@/plugins/kv_oauth.ts"; export interface State { sessionUser?: User; @@ -12,24 +12,21 @@ export interface State { export type SignedInState = Required; -export function assertSignedIn( - ctx: { state: State }, -): asserts ctx is { state: SignedInState } { +export function assertSignedIn(ctx: { + state: State; +}): asserts ctx is { state: SignedInState } { if (ctx.state.sessionUser === undefined) { throw new UnauthorizedError("User must be signed in"); } } -async function setSessionState( - req: Request, - ctx: FreshContext, -) { +async function setSessionState(req: Request, ctx: FreshContext) { if (ctx.destination !== "route") return await ctx.next(); // Initial state ctx.state.sessionUser = undefined; - const sessionId = getSessionId(req); + const sessionId = await getSessionId(req); if (sessionId === undefined) return await ctx.next(); const user = await getUserBySession(sessionId); if (user === null) return await ctx.next(); @@ -39,10 +36,7 @@ async function setSessionState( return await ctx.next(); } -async function ensureSignedIn( - _req: Request, - ctx: FreshContext, -) { +async function ensureSignedIn(_req: Request, ctx: FreshContext) { assertSignedIn(ctx); return await ctx.next(); } diff --git a/utils/github.ts b/utils/github.ts index 6b7f1ff15..2b6e36dc0 100644 --- a/utils/github.ts +++ b/utils/github.ts @@ -1,5 +1,5 @@ // Copyright 2023-2024 the Deno authors. All rights reserved. MIT license. -import { createGitHubOAuthConfig } from "kv_oauth/mod.ts"; +import { createGitHubOAuthConfig } from "jsr:@deno/kv-oauth"; import { BadRequestError } from "@/utils/http.ts"; export function isGitHubSetup() { @@ -39,5 +39,5 @@ export async function getGitHubUser(accessToken: string) { const { message } = await resp.json(); throw new BadRequestError(message); } - return await resp.json() as Promise; + return (await resp.json()) as Promise; } From 7f5970c2f33438e3201a1a3e1359191f17817a67 Mon Sep 17 00:00:00 2001 From: fdemir Date: Sun, 14 Apr 2024 01:43:04 +0300 Subject: [PATCH 2/3] fix: format --- plugins/kv_oauth.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/kv_oauth.ts b/plugins/kv_oauth.ts index 822833e48..9250ea3f5 100644 --- a/plugins/kv_oauth.ts +++ b/plugins/kv_oauth.ts @@ -11,7 +11,7 @@ import { isStripeEnabled, stripe } from "@/utils/stripe.ts"; import { getGitHubUser } from "@/utils/github.ts"; export const { signIn, handleCallback, signOut, getSessionId } = createHelpers( - createGitHubOAuthConfig() + createGitHubOAuthConfig(), ); // Exported for mocking and spying in e2e tests export const _internals = { handleCallback }; @@ -35,7 +35,7 @@ export default { path: "/callback", handler: async (req) => { const { response, tokens, sessionId } = await _internals.handleCallback( - req + req, ); const githubUser = await getGitHubUser(tokens.accessToken); From 68eed25d4cdeb6473bb7043dc81d48858dc14ad0 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 15 Apr 2024 08:47:43 +1000 Subject: [PATCH 3/3] fixes --- deno.json | 2 +- plugins/kv_oauth.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/deno.json b/deno.json index 9c8e35ef2..4f6c1a6dd 100644 --- a/deno.json +++ b/deno.json @@ -8,7 +8,7 @@ "db:migrate": "deno run --allow-read --allow-env --allow-net --unstable-kv tasks/db_migrate.ts", "db:reset": "deno run --allow-read --allow-env --unstable-kv tasks/db_reset.ts", "start": "deno run --unstable-kv -A --watch=static/,routes/ --env dev.ts", - "test": "DENO_KV_PATH=:memory: deno test -A --parallel --unstable-kv --coverage", + "test": "DENO_KV_PATH=:memory: deno test -A --parallel --unstable-kv --coverage --env", "check:license": "deno run --allow-read --allow-write tasks/check_license.ts", "check:types": "deno check **/*.ts && deno check **/*.tsx", "ok": "deno fmt --check && deno lint && deno task check:license --check && deno task check:types && deno task test", diff --git a/plugins/kv_oauth.ts b/plugins/kv_oauth.ts index 9250ea3f5..3c0e3a66f 100644 --- a/plugins/kv_oauth.ts +++ b/plugins/kv_oauth.ts @@ -61,7 +61,7 @@ export default { }, { path: "/signout", - handler: signOut, + handler: async (req) => await signOut(req), }, ], } as Plugin;