carnet api

This commit is contained in:
alma 2025-04-20 13:29:52 +02:00
parent 20ae56ce49
commit 9b6eed1b37

View File

@ -2,14 +2,12 @@ import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { DOMParser } from '@xmldom/xmldom';
import { cookies } from 'next/headers';
export async function GET() {
try {
const session = await getServerSession(authOptions);
const cookieStore = cookies();
if (!session?.user?.email) {
if (!session?.user?.email || !session?.accessToken) {
return NextResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
@ -36,35 +34,16 @@ export async function GET() {
}
try {
// Get user's folders using WebDAV with session cookies
// Get user's folders using WebDAV with Keycloak token
const webdavUrl = `${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(session.user.email)}/`;
console.log('Requesting WebDAV URL:', webdavUrl);
// Get all cookies from the request
const allCookies = cookieStore.getAll();
console.log('Available cookies:', allCookies.map(c => ({
name: c.name,
value: c.value.substring(0, 10) + '...', // Log partial value for security
domain: c.domain,
path: c.path,
secure: c.secure,
httpOnly: c.httpOnly,
sameSite: c.sameSite
})));
const cookieHeader = allCookies
.map(cookie => `${cookie.name}=${cookie.value}`)
.join('; ');
console.log('Sending cookie header:', cookieHeader.substring(0, 100) + '...'); // Log partial header
const foldersResponse = await fetch(webdavUrl, {
headers: {
'Cookie': cookieHeader,
'Authorization': `Bearer ${session.accessToken}`,
'Depth': '1',
'Content-Type': 'application/xml',
},
credentials: 'include', // Important for cookie handling
});
if (!foldersResponse.ok) {