feat: implement local caching for vCon data
Persist clients, storage stats, and file lists in local cache to improve initial load performance. Added breadcrumb navigation to the AdminPageHeader.
This commit is contained in:
1 parent
b42029f67d
commit
1a1c19d7bb
25 files changed
+892
-342
No files matched your search
@@ -46,9 +46,12 @@ export async function createAdminSessionToken(
|
||||
return `${payload}.${encodeBase64Url(new Uint8Array(signature))}`;
|
||||
}
|
||||
|
||||
export async function verifyAdminSessionToken(token: string, secret: string) {
|
||||
export async function getAdminSessionPayload(
|
||||
token: string,
|
||||
secret: string,
|
||||
): Promise<AdminSessionPayload | null> {
|
||||
const [payload, signature, extra] = token.split(".");
|
||||
if (!payload || !signature || extra) return false;
|
||||
if (!payload || !signature || extra) return null;
|
||||
|
||||
try {
|
||||
const valid = await crypto.subtle.verify(
|
||||
@@ -57,21 +60,29 @@ export async function verifyAdminSessionToken(token: string, secret: string) {
|
||||
decodeBase64Url(signature),
|
||||
encoder.encode(payload),
|
||||
);
|
||||
if (!valid) return false;
|
||||
if (!valid) return null;
|
||||
|
||||
const session = JSON.parse(
|
||||
new TextDecoder().decode(decodeBase64Url(payload)),
|
||||
) as AdminSessionPayload;
|
||||
return (
|
||||
if (
|
||||
typeof session.email === "string" &&
|
||||
typeof session.expiresAt === "number" &&
|
||||
session.expiresAt > Date.now()
|
||||
);
|
||||
) {
|
||||
return session;
|
||||
}
|
||||
return null;
|
||||
} catch {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function verifyAdminSessionToken(token: string, secret: string) {
|
||||
const session = await getAdminSessionPayload(token, secret);
|
||||
return Boolean(session);
|
||||
}
|
||||
|
||||
export function getSessionCookieOptions(request: Request, maxAge: number) {
|
||||
const forwardedProto = request.headers
|
||||
.get("x-forwarded-proto")
|
||||
|
||||
@@ -468,3 +468,83 @@ export async function verifyAdminCredentials(email: string, password: string) {
|
||||
const actualHash = await derivePasswordHash(password, inMem.passwordSalt);
|
||||
return timingSafeEqual(actualHash, inMem.passwordHash);
|
||||
}
|
||||
|
||||
export async function getAdminAccountInfo(fallbackEmail?: string) {
|
||||
const pool = getPool();
|
||||
if (pool) {
|
||||
try {
|
||||
await ensureAdminTable();
|
||||
const result = await pool.query(
|
||||
`SELECT email, created_at FROM admin_users WHERE id = 1`,
|
||||
);
|
||||
if (result.rowCount && result.rowCount > 0) {
|
||||
const row = result.rows[0] as { email: string; created_at: Date | string };
|
||||
return {
|
||||
email: row.email,
|
||||
createdAt: new Date(row.created_at).toISOString(),
|
||||
storageMode: "PostgreSQL" as const,
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// Fall back to in-memory admin
|
||||
}
|
||||
}
|
||||
|
||||
const inMem = await getOrInitInMemoryAdmin();
|
||||
return {
|
||||
email: fallbackEmail || inMem.email,
|
||||
createdAt: inMem.createdAt.toISOString(),
|
||||
storageMode: "In-Memory" as const,
|
||||
};
|
||||
}
|
||||
|
||||
export async function updateAdminAccount({
|
||||
currentEmail,
|
||||
currentPassword,
|
||||
nextEmail,
|
||||
nextPassword,
|
||||
}: {
|
||||
currentEmail: string;
|
||||
currentPassword: string;
|
||||
nextEmail: string;
|
||||
nextPassword?: string;
|
||||
}) {
|
||||
const verified = await verifyAdminCredentials(currentEmail, currentPassword);
|
||||
if (!verified) {
|
||||
return { ok: false as const, error: "Current password is incorrect." };
|
||||
}
|
||||
|
||||
const cleanEmail = nextEmail.trim().toLowerCase();
|
||||
const passwordToSave =
|
||||
nextPassword && nextPassword.length > 0 ? nextPassword : currentPassword;
|
||||
const salt = randomBytes(PASSWORD_SALT_BYTES);
|
||||
const passwordHash = await derivePasswordHash(passwordToSave, salt);
|
||||
|
||||
const pool = getPool();
|
||||
if (pool) {
|
||||
try {
|
||||
await ensureAdminTable();
|
||||
const updated = await pool.query(
|
||||
`UPDATE admin_users
|
||||
SET email = $1, password_salt = $2, password_hash = $3
|
||||
WHERE id = 1`,
|
||||
[cleanEmail, salt, passwordHash],
|
||||
);
|
||||
if (updated.rowCount && updated.rowCount > 0) {
|
||||
return { ok: true as const, email: cleanEmail };
|
||||
}
|
||||
} catch {
|
||||
// Fall back to in-memory update
|
||||
}
|
||||
}
|
||||
|
||||
const existing = await getOrInitInMemoryAdmin();
|
||||
globals.__dataStackInMemoryAdmin = {
|
||||
id: 1,
|
||||
email: cleanEmail,
|
||||
passwordSalt: salt,
|
||||
passwordHash,
|
||||
createdAt: existing.createdAt,
|
||||
};
|
||||
return { ok: true as const, email: cleanEmail };
|
||||
}
|
||||
Reference in new issue
Block a user