This commit is contained in:
jason
2026-03-13 16:00:33 -05:00
parent e08a2375ae
commit f1a3a31a94
3 changed files with 59 additions and 17 deletions
+8 -10
View File
@@ -5,7 +5,7 @@ export const runtime = "nodejs";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { uploadToDrive, updateDriveFile, generateReportMarkdown } from "@/lib/google-drive";
import { uploadToDrive, updateDriveFile, generateReportMarkdown, getGoogleAuth } from "@/lib/google-drive";
export async function POST(
req: Request,
@@ -18,13 +18,11 @@ export async function POST(
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
// With database sessions (not JWT), the Google access token lives in the
// Account table — getToken() returns null in this strategy.
const account = await prisma.account.findFirst({
where: { userId: session.user.id, provider: "google" },
});
if (!account?.access_token) {
let auth;
try {
auth = await getGoogleAuth(session.user.id);
} catch (error) {
console.error("Failed to get Google Auth:", error);
return NextResponse.json({ error: "Unauthorized or missing Google token" }, { status: 401 });
}
@@ -50,10 +48,10 @@ export async function POST(
if (report.driveFileId) {
// Update the existing Drive file in place
driveFile = await updateDriveFile(account.access_token, report.driveFileId, markdown);
driveFile = await updateDriveFile(auth, report.driveFileId, markdown);
} else {
// First export — create a new Drive file and store its ID
driveFile = await uploadToDrive(account.access_token, fileName, markdown, folderSetting?.value);
driveFile = await uploadToDrive(auth, fileName, markdown, folderSetting?.value);
await prisma.report.update({
where: { id },
data: { driveFileId: driveFile.id },