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