From c771628263a93764bbaef0c82ab21ec09cf44c78 Mon Sep 17 00:00:00 2001
From: Daniel Flanagan
Date: Tue, 11 Oct 2022 17:12:32 -0500
Subject: [PATCH] Closer
---
db/migrations.ts | 2 +-
routes/_app.tsx | 5 ++---
routes/_middleware.ts | 17 ++++++++++++-----
routes/index.tsx | 5 ++---
routes/login.tsx | 27 +++++++++++----------------
routes/note.tsx | 5 ++---
routes/note/[id].tsx | 5 ++---
routes/note/create.tsx | 5 ++---
routes/register.tsx | 37 +++++++++++++++----------------------
types.ts | 3 ---
10 files changed, 49 insertions(+), 62 deletions(-)
diff --git a/db/migrations.ts b/db/migrations.ts
index 6a0a4e8..df34f91 100644
--- a/db/migrations.ts
+++ b/db/migrations.ts
@@ -40,7 +40,7 @@ const tables: Record = {
...timestamps,
],
additionalTableStatements: [
- "constraint valid_username check (username ~* '^[a-z\d\\-_]{2,38}$')",
+ "constraint valid_username check (username ~* '^[a-z\\d\\-_]{2,38}$')",
],
},
"user_token": {
diff --git a/routes/_app.tsx b/routes/_app.tsx
index 2ada14d..79bf24a 100644
--- a/routes/_app.tsx
+++ b/routes/_app.tsx
@@ -8,9 +8,8 @@ interface MyAppProps extends AppProps {
}
export const handler: Handlers = {
- async GET(_request: Request, context) {
- const user: Partial = context.state.user;
- if (user && "passwordDigest" in user) delete user.passwordDigest;
+ async GET(request: Request, context) {
+ console.log("AppHandler:", request, context);
return await context.render(context.state.user);
},
};
diff --git a/routes/_middleware.ts b/routes/_middleware.ts
index 4a0d90a..9108594 100644
--- a/routes/_middleware.ts
+++ b/routes/_middleware.ts
@@ -1,7 +1,17 @@
import { MiddlewareHandlerContext } from "$fresh/server.ts";
import { deleteCookie, getCookies } from "$std/http/cookie.ts";
import { getUserFromNonExpiredLoginToken } from "@/db/mod.ts";
-import { type ContextState } from "@/types.ts";
+import { type ContextState, type PublicUser, type User } from "@/types.ts";
+
+function toPublicUser(user: User): PublicUser {
+ const {
+ createdAt: _createdAt,
+ updatedAt: _updatedAt,
+ passwordDigest: _passwordDigest,
+ ...publicUser
+ } = user;
+ return publicUser;
+}
async function currentUser(
request: Request,
@@ -14,10 +24,7 @@ async function currentUser(
const user = await getUserFromNonExpiredLoginToken(lsauth);
if (!user) hasBadAuthCookie = true;
else {
- context.state.user = user;
- delete context.state.user.createdAt;
- delete context.state.user.updatedAt;
- delete context.state.user.passwordDigest;
+ context.state.user = toPublicUser(user);
}
}
const resp = await context.next();
diff --git a/routes/index.tsx b/routes/index.tsx
index b512209..cd244bd 100644
--- a/routes/index.tsx
+++ b/routes/index.tsx
@@ -1,15 +1,14 @@
import Counter from "@/islands/Counter.tsx";
-import { Page } from "@/components/Page.tsx";
export default function Home() {
return (
-
+ <>
-
+ >
);
}
diff --git a/routes/login.tsx b/routes/login.tsx
index 20fbfd5..88b927b 100644
--- a/routes/login.tsx
+++ b/routes/login.tsx
@@ -1,6 +1,5 @@
import { HandlerContext, Handlers, PageProps } from "$fresh/server.ts";
import { compare } from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";
-import { Page } from "@/components/Page.tsx";
import { createToken, getUser } from "@/db/mod.ts";
import * as base64 from "$std/encoding/base64.ts";
import { setCookie } from "$std/http/cookie.ts";
@@ -58,18 +57,16 @@ export default function Login({ data }: PageProps) {
function LoginSuccessful(_userId: UserID) {
return (
-
-
- You are now logged in. Let's go to your{" "}
- dashboard!
-
-
+
+ You are now logged in. Let's go to your{" "}
+ dashboard!
+
);
}
function LoginForm(props?: LoginError | null) {
return (
-
+
-
+
+
+
+
+
+
);
}
diff --git a/routes/note.tsx b/routes/note.tsx
index ae3da0e..9592e4e 100644
--- a/routes/note.tsx
+++ b/routes/note.tsx
@@ -1,6 +1,5 @@
import { Handlers, PageProps } from "$fresh/server.ts";
import { listNotes } from "@/db/mod.ts";
-import { Page } from "@/components/Page.tsx";
import { type Note } from "@/types.ts";
import { NoteItem } from "@/components/Note.tsx";
@@ -12,7 +11,7 @@ export const handler: Handlers = {
export default function NotesPage({ data: notes }: PageProps) {
return (
-
+ <>
List of Notes
Create a note:
{notes.map(NoteItem)}
-
+ >
);
}
diff --git a/routes/note/[id].tsx b/routes/note/[id].tsx
index 7784ca0..e62f862 100644
--- a/routes/note/[id].tsx
+++ b/routes/note/[id].tsx
@@ -1,6 +1,5 @@
import { Handlers, PageProps } from "$fresh/server.ts";
import { getNote } from "@/db/mod.ts";
-import { Page } from "@/components/Page.tsx";
import { type Note } from "@/types.ts";
export const handler: Handlers = {
@@ -16,7 +15,7 @@ export default function NotesPage(
{ data: { id, createdAt, content } }: PageProps,
) {
return (
-
+ <>
Back to notes
Note {id} created at {createdAt.toLocaleString()}
@@ -24,6 +23,6 @@ export default function NotesPage(
{content}
-
+ >
);
}
diff --git a/routes/note/create.tsx b/routes/note/create.tsx
index 197619f..a8f651e 100644
--- a/routes/note/create.tsx
+++ b/routes/note/create.tsx
@@ -1,6 +1,5 @@
import { Handlers, PageProps } from "$fresh/server.ts";
import { createNote } from "@/db/mod.ts";
-import { Page } from "@/components/Page.tsx";
import { type ContextState, type Note } from "@/types.ts";
export const handler: Handlers = {
@@ -16,10 +15,10 @@ export const handler: Handlers = {
export default function NotesPage({ data: { id } }: PageProps) {
return (
-
+ <>
You created a note!
Back to notes
View your note
-
+ >
);
}
diff --git a/routes/register.tsx b/routes/register.tsx
index 11068d0..1771b3a 100644
--- a/routes/register.tsx
+++ b/routes/register.tsx
@@ -1,10 +1,7 @@
import { Handlers, PageProps } from "$fresh/server.ts";
-import { Page } from "@/components/Page.tsx";
import { createUser, PostgresError } from "@/db/mod.ts";
import { hash } from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";
-type UserID = string;
-
interface RegistrationError {
message: string;
}
@@ -31,7 +28,7 @@ export const handler: Handlers = {
if (!result) throw "insert failed";
const [user, _team] = result;
if (!user) throw "insert failed";
- return await context.render(id);
+ return await context.render(user.id);
} catch (err) {
if (
err instanceof PostgresError && err.fields.code == "23505" &&
@@ -66,18 +63,16 @@ export default function Register(
function RegistrationSuccessful(_userId: UserID) {
return (
-
-
- You're all signed up! Let's go log in!
-
-
+
+ You're all signed up! Let's go log in!
+
);
}
function RegistrationForm(props?: RegistrationError | null) {
console.log(props);
return (
-
+
)}
-
-
+
+
+
+
+
+
);
}
diff --git a/types.ts b/types.ts
index 959386c..7f714c4 100644
--- a/types.ts
+++ b/types.ts
@@ -51,6 +51,3 @@ export type TokenDigest = string;
export interface ContextState {
user?: PublicUser;
}
-
-function toPublicUser(user: User): PublicUser {
-}