Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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/"
},
Expand Down
15 changes: 6 additions & 9 deletions plugins/kv_oauth.ts
Original file line number Diff line number Diff line change
@@ -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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { createGitHubOAuthConfig, createHelpers } from "jsr:@deno/kv-oauth";
import { createGitHubOAuthConfig, createHelpers } from "@deno/kv-oauth";

Ditto for other imports. Please run deno add jsr:@deno/kv-oauth to add the import to the deno.json file.

import {
createUser,
getUser,
Expand All @@ -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 };

Expand All @@ -31,14 +29,13 @@ export default {
routes: [
{
path: "/signin",
handler: async (req) => await signIn(req, createGitHubOAuthConfig()),
handler: async (req) => await signIn(req),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
handler: async (req) => await signIn(req),
handler: (req) => signIn(req),

Nit

},
{
path: "/callback",
handler: async (req) => {
const { response, tokens, sessionId } = await _internals.handleCallback(
req,
createGitHubOAuthConfig(),
);

const githubUser = await getGitHubUser(tokens.accessToken);
Expand All @@ -64,7 +61,7 @@ export default {
},
{
path: "/signout",
handler: signOut,
handler: async (req) => await signOut(req),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
handler: async (req) => await signOut(req),
handler: (req) => signOut(req),

Nit

},
],
} as Plugin;
20 changes: 7 additions & 13 deletions plugins/session.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
// 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;
}

export type SignedInState = Required<State>;

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<State>,
) {
async function setSessionState(req: Request, ctx: FreshContext<State>) {
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();
Expand All @@ -39,10 +36,7 @@ async function setSessionState(
return await ctx.next();
}

async function ensureSignedIn(
_req: Request,
ctx: FreshContext<State>,
) {
async function ensureSignedIn(_req: Request, ctx: FreshContext<State>) {
assertSignedIn(ctx);
return await ctx.next();
}
Expand Down
4 changes: 2 additions & 2 deletions utils/github.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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<GitHubUser>;
return (await resp.json()) as Promise<GitHubUser>;
}