Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

fix: verify the session user exists, guard backup import and sign out stale dashboard sessions

Khuram Niaz936cc85a91d9
diff --git a/__tests__/StaleSessionSignOut.spec.tsx b/__tests__/StaleSessionSignOut.spec.tsx
new file mode 100644
index 0000000..9a6b084
--- /dev/null
+++ b/__tests__/StaleSessionSignOut.spec.tsx
@@ -0,0 +1,12 @@
+import { render, screen, waitFor } from "@testing-library/react";
+import StaleSessionSignOut from "@/components/StaleSessionSignOut";
+
+describe("StaleSessionSignOut", () => {
+ it("clears the session on mount rather than only redirecting", async () => {
+ const signOutAction = vi.fn().mockResolvedValue(undefined);
+ render(<StaleSessionSignOut signOutAction={signOutAction} />);
+
+ await waitFor(() => expect(signOutAction).toHaveBeenCalled());
+ expect(screen.getByText(/signing you out/i)).toBeInTheDocument();
+ });
+});
diff --git a/__tests__/backupImportGuards.spec.ts b/__tests__/backupImportGuards.spec.ts
index 55fdf2d..9461fdd 100644
--- a/__tests__/backupImportGuards.spec.ts
+++ b/__tests__/backupImportGuards.spec.ts
@@ -71,6 +71,18 @@ describe("importBackup guards", () => {
expect(JSON.stringify(where)).toContain("user-1");
});
+ // A JWT outlives the database it was minted against: a fresh container with
+ // a new dev.db still accepts the old cookie, and every scoped read comes
+ // back empty rather than failing.
+ it("refuses a session whose user row no longer exists", async () => {
+ mockDb.user.findUnique.mockResolvedValueOnce(null);
+ await expect(
+ importBackup(await emptyBackup(), "user-1", EMAIL, true),
+ ).rejects.toThrow(/sign in again/i);
+ expect(mockDb.job.deleteMany).not.toHaveBeenCalled();
+ expect(writeSnapshot).not.toHaveBeenCalled();
+ });
+
it("refuses a payload whose data.json fails validation", async () => {
const zip = new JSZip();
const data = BackupDataSchema.parse({});
diff --git a/src/app/dashboard/layout.tsx b/src/app/dashboard/layout.tsx
index bd5a5f7..4caab31 100644
--- a/src/app/dashboard/layout.tsx
+++ b/src/app/dashboard/layout.tsx
@@ -11,6 +11,7 @@ import { AgentChatPanel } from "@/components/agent/AgentChatPanel";
import { getChatConversation } from "@/actions/agentChat.actions";
import { APP_CONSTANTS } from "@/lib/constants";
import { getCurrentUser } from "@/utils/user.utils";
+import StaleSessionSignOut from "@/components/StaleSessionSignOut";
import { signOut } from "@/auth";
export default async function RootLayout({
@@ -27,19 +28,25 @@ export default async function RootLayout({
const conversation = await getChatConversation();
const user = await getCurrentUser();
+ const signOutAction = async () => {
+ "use server";
+ await signOut({ redirectTo: "/signin" });
+ };
+
+ // The session outlived the database it was minted against: middleware still
+ // passes the signed cookie, but the user row is gone and every scoped query
+ // would read as empty rather than unauthorized.
+ if (!user) {
+ return <StaleSessionSignOut signOutAction={signOutAction} />;
+ }
+
return (
<ActivityProvider>
<SidebarProvider initialExpanded={initialExpanded}>
<RightRailProvider>
<AgentChatProvider initialMessages={conversation.data ?? []}>
<div className="flex min-h-screen w-full flex-col bg-muted/40">
- <Sidebar
- user={user}
- signOutAction={async () => {
- "use server";
- await signOut({ redirectTo: "/signin" });
- }}
- />
+ <Sidebar user={user} signOutAction={signOutAction} />
<SidebarInset>
<Header />
<GlobalActivityBanner />
diff --git a/src/components/StaleSessionSignOut.tsx b/src/components/StaleSessionSignOut.tsx
new file mode 100644
index 0000000..180a162
--- /dev/null
+++ b/src/components/StaleSessionSignOut.tsx
@@ -0,0 +1,26 @@
+"use client";
+
+import { useEffect } from "react";
+
+// Clearing the cookie is the only way out of a session whose user row is
+// gone: redirecting alone loops, because auth.config sends anyone holding a
+// valid token back to /dashboard.
+function StaleSessionSignOut({
+ signOutAction,
+}: {
+ signOutAction: () => Promise<void>;
+}) {
+ useEffect(() => {
+ signOutAction();
+ }, [signOutAction]);
+
+ return (
+ <div className="flex min-h-screen w-full items-center justify-center bg-muted/40">
+ <p className="text-sm text-muted-foreground">
+ Your session is no longer valid. Signing you out&hellip;
+ </p>
+ </div>
+ );
+}
+
+export default StaleSessionSignOut;
diff --git a/src/lib/backup/import.ts b/src/lib/backup/import.ts
index ec3fc8e..f2e99dc 100644
--- a/src/lib/backup/import.ts
+++ b/src/lib/backup/import.ts
@@ -186,6 +186,21 @@ export async function importBackup(
userEmail: string,
confirmWipe: boolean,
): Promise<ImportResult> {
+ // A JWT session outlives the database it was minted against: a container
+ // with a fresh dev.db still accepts the old cookie, and every scoped read
+ // returns empty instead of failing. wipe()'s user.update is the only
+ // statement that needs the row itself, so without this the import dies
+ // mid-transaction on an opaque P2025.
+ const owner = await db.user.findUnique({
+ where: { id: userId },
+ select: { id: true },
+ });
+ if (!owner) {
+ throw new BackupError(
+ "Your sign-in is out of date. Sign out, sign in again, then retry the import.",
+ );
+ }
+
const zip = await openBackupZip(bytes);
await readManifest(zip);
const data = await readData(zip);
diff --git a/src/utils/user.utils.ts b/src/utils/user.utils.ts
index 9f6bb79..6104397 100644
--- a/src/utils/user.utils.ts
+++ b/src/utils/user.utils.ts
@@ -1,10 +1,19 @@
import "server-only";
import { auth } from "@/auth";
+import prisma from "@/lib/db";
import { CurrentUser } from "@/models/user.model";
export const getCurrentUser = async () => {
const session = await auth();
if (!session?.user?.id) return null;
+ // A JWT carries no proof the row still exists. Replace the database and the
+ // signed cookie points at nothing, which every ownership-scoped query reads
+ // as "no data" rather than "not signed in".
+ const exists = await prisma.user.findUnique({
+ where: { id: session.user.id },
+ select: { id: true },
+ });
+ if (!exists) return null;
const user: CurrentUser = {
id: session.user.id,
name: session.user.name ?? "",