404 pages have context state
This commit is contained in:
parent
e667f39852
commit
32068b3b19
5 changed files with 15 additions and 43 deletions
26
db/mod.ts
26
db/mod.ts
|
@ -52,7 +52,6 @@ async function rowExists(
|
|||
args,
|
||||
);
|
||||
if (result && result.rows.length > 0) {
|
||||
log.info("rowExists result:", result.rows);
|
||||
return !!(result.rows[0][0]);
|
||||
}
|
||||
return false;
|
||||
|
@ -125,14 +124,13 @@ export async function dbOp<T>(
|
|||
try {
|
||||
result = await op(connection);
|
||||
} catch (err) {
|
||||
log.error("Error querying database:", err);
|
||||
exception = err;
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
} catch (err) {
|
||||
exception = err;
|
||||
log.critical("Error connecting to database:", err);
|
||||
exception = err;
|
||||
}
|
||||
if (exception != null) throw exception;
|
||||
if (result == null) {
|
||||
|
@ -154,13 +152,11 @@ export async function queryObject<T>(
|
|||
return await queryObject(sql, args, connection);
|
||||
});
|
||||
} else {
|
||||
log.debug(`queryObject: ${sql}`);
|
||||
const result = await connection.queryObject<T>({
|
||||
camelcase: true,
|
||||
text: sql.trim(),
|
||||
args,
|
||||
});
|
||||
log.debug("queryObject Result:", result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -178,12 +174,10 @@ export async function queryArray<T extends unknown[]>(
|
|||
return await queryArray<T>(sql, args, connection);
|
||||
});
|
||||
} else {
|
||||
log.debug(`queryArray: ${sql}`);
|
||||
const result = await connection.queryArray<T>({
|
||||
text: sql.trim(),
|
||||
args,
|
||||
});
|
||||
log.debug("queryArray Result:", result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -200,7 +194,6 @@ export async function getNote(
|
|||
id: string | { id: string },
|
||||
): Promise<Note> {
|
||||
const idVal = typeof id == "object" ? id.id : id;
|
||||
log.debug("getNote id =", JSON.stringify(idVal));
|
||||
return singleRow(
|
||||
await queryObject<Note>(
|
||||
"select * from note where id = $1",
|
||||
|
@ -246,7 +239,6 @@ export async function createTeam(
|
|||
},
|
||||
transaction?: Transaction,
|
||||
): Promise<Team> {
|
||||
log.debug("createTeam tx:", transaction);
|
||||
if (!transaction) {
|
||||
return await wrapWithTransaction<Team>(
|
||||
"createTeam",
|
||||
|
@ -270,7 +262,6 @@ export async function createTeam(
|
|||
}
|
||||
return team;
|
||||
} catch (e) {
|
||||
log.error("Error creating team:", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
@ -289,22 +280,14 @@ export async function wrapWithTransaction<T>(
|
|||
);
|
||||
try {
|
||||
await transaction.begin();
|
||||
log.debug(
|
||||
`started ${transactionName} tx with options ${
|
||||
JSON.stringify(transactionOptions)
|
||||
}:`,
|
||||
transaction,
|
||||
);
|
||||
const result: T = await callback(transaction);
|
||||
await transaction.commit();
|
||||
return result;
|
||||
} catch (e) {
|
||||
await transaction.rollback();
|
||||
log.error("Failed to complete transaction:", e);
|
||||
throw e;
|
||||
}
|
||||
} catch (e) {
|
||||
log.error("Failed to create transaction");
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
|
@ -340,7 +323,6 @@ export async function createUser(
|
|||
|
||||
return user;
|
||||
} catch (e) {
|
||||
log.error("Error creating user:", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
@ -361,12 +343,6 @@ 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<Token>(
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
"imports": {
|
||||
"@/": "./",
|
||||
"$std/": "https://deno.land/std@0.162.0/",
|
||||
"$fresh/": "https://raw.githubusercontent.com/lytedev/fresh/v1.1.2-df/",
|
||||
"$fresh/": "https://raw.githubusercontent.com/lytedev/fresh/1.1.2-df/",
|
||||
"$freshrel/": "../fresh/",
|
||||
"preact": "https://esm.sh/preact@10.11.0",
|
||||
"preact/": "https://esm.sh/preact@10.11.0/",
|
||||
|
|
|
@ -40,6 +40,7 @@ export function UserNavItems() {
|
|||
export default function App(
|
||||
{ Component, contextState }: AppProps<ContextState>,
|
||||
) {
|
||||
console.log("contextState", contextState);
|
||||
return (
|
||||
<div class="relative min-h-screen flex flex-col">
|
||||
<header class="flex justify-start items-center">
|
||||
|
|
|
@ -20,7 +20,11 @@ async function currentUser(
|
|||
) {
|
||||
let hasBadAuthCookie = false;
|
||||
const { lsauth } = getCookies(request.headers);
|
||||
log.debug("lsauth cookie:", lsauth);
|
||||
const url = new URL(request.url);
|
||||
if (!url.pathname.startsWith("/_frsh/") || url.pathname == "favicon.ico") {
|
||||
// don't log certain requests' cookies
|
||||
log.info("lsauth cookie:", lsauth);
|
||||
}
|
||||
if (lsauth) {
|
||||
try {
|
||||
context.state.user = toPublicUser(
|
||||
|
@ -30,9 +34,10 @@ async function currentUser(
|
|||
hasBadAuthCookie = true;
|
||||
}
|
||||
}
|
||||
log.info(context.state);
|
||||
const resp = await context.next();
|
||||
if (resp) {
|
||||
if (hasBadAuthCookie) deleteCookie(resp.headers, "lsauth");
|
||||
if (resp && hasBadAuthCookie) {
|
||||
deleteCookie(resp.headers, "lsauth");
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
@ -48,16 +53,7 @@ export async function serverHeader(
|
|||
return resp;
|
||||
}
|
||||
|
||||
export async function someState(
|
||||
_request: Request,
|
||||
context: MiddlewareHandlerContext<ContextState>,
|
||||
) {
|
||||
context.state.something = "I said something!";
|
||||
return await context.next();
|
||||
}
|
||||
|
||||
export const handler = [
|
||||
someState,
|
||||
currentUser,
|
||||
serverHeader,
|
||||
];
|
||||
|
|
|
@ -21,13 +21,12 @@ export const handler: Handlers<TeamPageProps, ContextState> = {
|
|||
// NOTE: maybe teams can be public...?
|
||||
const { id } = context.params;
|
||||
|
||||
if (!await isUserInTeam(context.state.user?.id, id)) {
|
||||
// users that are not a member of a team may not view it
|
||||
return await context.renderNotFound();
|
||||
}
|
||||
|
||||
console.debug({ request, context });
|
||||
try {
|
||||
if (!await isUserInTeam(context.state.user?.id, id)) {
|
||||
// users that are not a member of a team may not view it
|
||||
return await context.renderNotFound();
|
||||
}
|
||||
const team = await getTeam({ id });
|
||||
const users = await getTeamUsers(team) || [];
|
||||
return await context.render({ team, users });
|
||||
|
|
Loading…
Reference in a new issue