diff --git a/db/mod.ts b/db/mod.ts index 9aa62b5..f36aa50 100644 --- a/db/mod.ts +++ b/db/mod.ts @@ -159,6 +159,12 @@ export async function createToken( if (!(digest instanceof Uint8Array)) throw "token digest was non-brinary"; intermediateToken.digest = digest; } + log.debug( + `intermediateToken bytes: ${base64.encode(intermediateToken.bytes)}`, + ); + log.debug( + `intermediateToken digest: ${base64.encode(intermediateToken.digest)}`, + ); if (!intermediateToken.data) intermediateToken.data = null; const result = singleRow( await queryObject( @@ -174,8 +180,20 @@ export async function createToken( return null; } +export async function deleteToken( + token: TokenDigest, +) { + const digest = sha256(base64.decode(token)); + return await queryObject( + ` + delete from user_token where digest = $1 + `, + [digest], + ); +} + export async function getToken(token: TokenDigest): Promise { - const digest = base64.decode(token); + const digest = sha256(base64.decode(token)); return singleRow( await queryObject( ` @@ -202,6 +220,7 @@ export async function getUser( export async function getUserFromNonExpiredLoginToken( token: TokenDigest, ): Promise { + // TODO: if the token has expired, return a specific error? const digest = sha256(base64.decode(token)); return singleRow( await queryObject( @@ -210,7 +229,7 @@ export async function getUserFromNonExpiredLoginToken( left join "user" u on u.id = ut.user_id where ut."digest" = $1 and ut."data"->>'type' = 'login' - and now() < (ut.created_at + '7 days'::interval) + and now() < (ut.created_at + '14 days'::interval) `, [digest], ), diff --git a/fresh.gen.ts b/fresh.gen.ts index b23516b..b023b8d 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -7,7 +7,7 @@ import * as $0 from "./routes/[name].tsx"; import * as $1 from "./routes/_404.tsx"; import * as $2 from "./routes/_500.tsx"; import * as $3 from "./routes/_app.tsx"; -import * as $4 from "./routes/_middleware.ts"; +import * as $4 from "./routes/_middleware.tsx"; import * as $5 from "./routes/about.tsx"; import * as $6 from "./routes/api/joke.ts"; import * as $7 from "./routes/api/random-uuid.ts"; @@ -33,7 +33,7 @@ const manifest = { "./routes/_404.tsx": $1, "./routes/_500.tsx": $2, "./routes/_app.tsx": $3, - "./routes/_middleware.ts": $4, + "./routes/_middleware.tsx": $4, "./routes/about.tsx": $5, "./routes/api/joke.ts": $6, "./routes/api/random-uuid.ts": $7, diff --git a/import_map.json b/import_map.json index fc7a3bd..8ac9a8f 100644 --- a/import_map.json +++ b/import_map.json @@ -2,7 +2,8 @@ "imports": { "@/": "./", "$std/": "https://deno.land/std@0.158.0/", - "$fresh/": "https://deno.land/x/fresh@1.1.2/", + "$freshbranch/": "https://raw.githubusercontent.com/lytedev/fresh/v1.1.2-df/", + "$fresh/": "../fresh/", "preact": "https://esm.sh/preact@10.11.0", "preact/": "https://esm.sh/preact@10.11.0/", "preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.4", diff --git a/routes/_app.tsx b/routes/_app.tsx index d9efe02..8252de2 100644 --- a/routes/_app.tsx +++ b/routes/_app.tsx @@ -2,18 +2,6 @@ import { type AppProps, Handlers } from "$fresh/server.ts"; import { type PublicUser } from "@/types.ts"; import { type ContextState } from "@/types.ts"; -interface MyAppProps extends AppProps { - user?: PublicUser; -} - -export const handler: Handlers = { - async GET(request: Request, context) { - console.error("\n\nYO\n\n"); - console.log("AppHandler:", request, context); - return await context.render({ user: context.state.user }); - }, -}; - const NAV_ITEM_CLASSES = "flex justify-center items-center px-4 py-2 hover:bg-gray-300 dark:hover:bg-gray-700"; @@ -50,8 +38,9 @@ export function UserNavItems() { ); } -export default function App({ Component, ...props }: MyAppProps) { - console.log("AppProps:", props); +export default function App( + { Component, contextState }: AppProps, +) { return (
@@ -64,11 +53,11 @@ export default function App({ Component, ...props }: MyAppProps) {

LyricScreen

Notes - {props.user ? UserNavItems() : LoginNavItems()} + {contextState.user ? UserNavItems() : LoginNavItems()}
- +