Closer
This commit is contained in:
parent
34b440213f
commit
c771628263
|
@ -40,7 +40,7 @@ const tables: Record<string, TableSpec> = {
|
|||
...timestamps,
|
||||
],
|
||||
additionalTableStatements: [
|
||||
"constraint valid_username check (username ~* '^[a-z\d\\-_]{2,38}$')",
|
||||
"constraint valid_username check (username ~* '^[a-z\\d\\-_]{2,38}$')",
|
||||
],
|
||||
},
|
||||
"user_token": {
|
||||
|
|
|
@ -8,9 +8,8 @@ interface MyAppProps extends AppProps {
|
|||
}
|
||||
|
||||
export const handler: Handlers<unknown, ContextState> = {
|
||||
async GET(_request: Request, context) {
|
||||
const user: Partial<ContextState["user"]> = 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);
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
import Counter from "@/islands/Counter.tsx";
|
||||
import { Page } from "@/components/Page.tsx";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<Page>
|
||||
<>
|
||||
<img
|
||||
src="/logo.svg"
|
||||
class="w-32 h-32"
|
||||
alt="the fresh logo: a sliced lemon dripping with juice"
|
||||
/>
|
||||
<Counter start={3} />
|
||||
</Page>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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 (
|
||||
<Page>
|
||||
<p>
|
||||
You are now logged in. Let's go to your{" "}
|
||||
<a href="/dashboard">dashboard</a>!
|
||||
</p>
|
||||
</Page>
|
||||
);
|
||||
}
|
||||
|
||||
function LoginForm(props?: LoginError | null) {
|
||||
return (
|
||||
<Page>
|
||||
<form class="flex flex-col max-w-lg" method="post">
|
||||
{props != null &&
|
||||
(
|
||||
<p class="text-red-500">
|
||||
|
@ -79,13 +76,11 @@ function LoginForm(props?: LoginError | null) {
|
|||
<h1 class="text-4xl mb-4 outline-white">
|
||||
Log in to your account
|
||||
</h1>
|
||||
<form class="flex flex-col max-w-lg" method="post">
|
||||
<label for="username">Username</label>
|
||||
<input type="text" name="username" />
|
||||
<label for="password">Password</label>
|
||||
<input type="password" name="password" />
|
||||
<input class="mt-2" type="submit" value="Login" />
|
||||
</form>
|
||||
</Page>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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<Note[]> = {
|
|||
|
||||
export default function NotesPage({ data: notes }: PageProps<Note[]>) {
|
||||
return (
|
||||
<Page>
|
||||
<>
|
||||
<h1>List of Notes</h1>
|
||||
<p>Create a note:</p>
|
||||
<form class="flex flex-col" action="./note/create" method="POST">
|
||||
|
@ -21,6 +20,6 @@ export default function NotesPage({ data: notes }: PageProps<Note[]>) {
|
|||
<input class="mt-2" type="submit" value="Post" />
|
||||
</form>
|
||||
{notes.map(NoteItem)}
|
||||
</Page>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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<Note> = {
|
||||
|
@ -16,7 +15,7 @@ export default function NotesPage(
|
|||
{ data: { id, createdAt, content } }: PageProps<Note>,
|
||||
) {
|
||||
return (
|
||||
<Page>
|
||||
<>
|
||||
<a href="/note">Back to notes</a>
|
||||
<h1>Note {id} created at {createdAt.toLocaleString()}</h1>
|
||||
<div class="my-4" key={id}>
|
||||
|
@ -24,6 +23,6 @@ export default function NotesPage(
|
|||
<pre>{content}</pre>
|
||||
</blockquote>
|
||||
</div>
|
||||
</Page>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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<Note, ContextState> = {
|
||||
|
@ -16,10 +15,10 @@ export const handler: Handlers<Note, ContextState> = {
|
|||
|
||||
export default function NotesPage({ data: { id } }: PageProps<Note>) {
|
||||
return (
|
||||
<Page>
|
||||
<>
|
||||
<h1>You created a note!</h1>
|
||||
<a href="/note">Back to notes</a>
|
||||
<a href={`/note/${id}`}>View your note</a>
|
||||
</Page>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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<UserID | RegistrationError | null> = {
|
|||
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 (
|
||||
<Page>
|
||||
<p>
|
||||
You're all signed up! Let's go <a href="/login">log in</a>!
|
||||
</p>
|
||||
</Page>
|
||||
);
|
||||
}
|
||||
|
||||
function RegistrationForm(props?: RegistrationError | null) {
|
||||
console.log(props);
|
||||
return (
|
||||
<Page>
|
||||
<form class="flex flex-col max-w-lg" method="post">
|
||||
<h1 class="text-4xl mb-4">Register your account</h1>
|
||||
{props != null &&
|
||||
(
|
||||
|
@ -85,7 +80,6 @@ function RegistrationForm(props?: RegistrationError | null) {
|
|||
<strong>Error</strong>: {props.message}
|
||||
</p>
|
||||
)}
|
||||
<form class="flex flex-col max-w-lg" method="post">
|
||||
<label for="username">Username</label>
|
||||
<input type="text" name="username" />
|
||||
<label for="password">Password</label>
|
||||
|
@ -96,6 +90,5 @@ function RegistrationForm(props?: RegistrationError | null) {
|
|||
value="Register"
|
||||
/>
|
||||
</form>
|
||||
</Page>
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue