diff --git a/app/api/nextcloud/files/route.ts b/app/api/nextcloud/files/route.ts index 894a49c6..c6e6f3db 100644 --- a/app/api/nextcloud/files/route.ts +++ b/app/api/nextcloud/files/route.ts @@ -1,7 +1,7 @@ import { NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { PrismaClient } from '@prisma/client'; -import { authOptions } from '@/lib/auth'; +import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { WebDAVClient } from 'webdav'; // Use a single PrismaClient instance @@ -15,7 +15,7 @@ if (process.env.NODE_ENV !== 'production') global.prisma = prisma; export async function GET(request: Request) { try { const session = await getServerSession(authOptions); - if (!session?.user?.email) { + if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } @@ -24,13 +24,17 @@ export async function GET(request: Request) { // Get WebDAV credentials const credentials = await prisma.webDAVCredentials.findUnique({ - where: { userId: session.user.email }, + where: { userId: session.user.id }, }); if (!credentials) { + console.error('No WebDAV credentials found for user:', session.user.id); return NextResponse.json({ error: 'No WebDAV credentials found' }, { status: 404 }); } + console.log('Using credentials for user:', credentials.username); + console.log('Accessing folder:', folder); + // Initialize WebDAV client const client = new WebDAVClient({ username: credentials.username, @@ -38,8 +42,12 @@ export async function GET(request: Request) { baseURL: process.env.NEXTCLOUD_URL, }); + const folderPath = `/remote.php/dav/files/${credentials.username}/Private/${folder}`; + console.log('Full folder path:', folderPath); + // List files in the specified folder - const files = await client.getDirectoryContents(`/remote.php/dav/files/${credentials.username}/Private/${folder}`); + const files = await client.getDirectoryContents(folderPath); + console.log('Found files:', files.length); // Filter for .md files and format the response const markdownFiles = files