Merge pull request 'fix: read Google access_token from Account table, not getToken()' (#12) from claude/reverent-proskuriakova into master
Reviewed-on: #12
This commit was merged in pull request #12.
This commit is contained in:
@@ -2,12 +2,10 @@ import { NextResponse } from "next/server";
|
|||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
|
|
||||||
|
|
||||||
import { getServerSession } from "next-auth/next";
|
import { getServerSession } from "next-auth/next";
|
||||||
import { authOptions } from "@/lib/auth";
|
import { authOptions } from "@/lib/auth";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { prisma } from "@/lib/prisma";
|
||||||
import { uploadToDrive, generateReportMarkdown } from "@/lib/google-drive";
|
import { uploadToDrive, generateReportMarkdown } from "@/lib/google-drive";
|
||||||
import { getToken } from "next-auth/jwt";
|
|
||||||
|
|
||||||
export async function POST(
|
export async function POST(
|
||||||
req: Request,
|
req: Request,
|
||||||
@@ -16,10 +14,17 @@ export async function POST(
|
|||||||
const { id } = await params;
|
const { id } = await params;
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
|
|
||||||
// We need the raw access token from JWT for Google API
|
if (!session?.user?.id) {
|
||||||
const token = await getToken({ req: req as any });
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
if (!session || !token?.accessToken) {
|
// 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) {
|
||||||
return NextResponse.json({ error: "Unauthorized or missing Google token" }, { status: 401 });
|
return NextResponse.json({ error: "Unauthorized or missing Google token" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +47,7 @@ export async function POST(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const driveFile = await uploadToDrive(
|
const driveFile = await uploadToDrive(
|
||||||
token.accessToken as string,
|
account.access_token,
|
||||||
fileName,
|
fileName,
|
||||||
markdown,
|
markdown,
|
||||||
folderSetting?.value
|
folderSetting?.value
|
||||||
|
|||||||
Reference in New Issue
Block a user