carnet api

This commit is contained in:
alma 2025-04-20 13:24:31 +02:00
parent 32ac30631d
commit ae5176e90a

View File

@ -2,47 +2,12 @@ import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth'; import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { DOMParser } from '@xmldom/xmldom'; import { DOMParser } from '@xmldom/xmldom';
import { cookies } from 'next/headers';
async function getNextcloudAccessToken(clientId: string, clientSecret: string) {
const nextcloudUrl = process.env.NEXTCLOUD_URL;
if (!nextcloudUrl) {
throw new Error('NEXTCLOUD_URL is not defined');
}
const tokenEndpoint = `${nextcloudUrl}/index.php/apps/oauth2/api/v1/token`;
const params = new URLSearchParams({
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
});
const response = await fetch(tokenEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
},
body: params.toString(),
});
if (!response.ok) {
const errorText = await response.text();
console.error('Nextcloud token response:', errorText);
throw new Error(`Failed to get Nextcloud access token: ${response.status} ${errorText}`);
}
const data = await response.json();
if (!data.access_token) {
throw new Error('No access token in response');
}
return data.access_token;
}
export async function GET() { export async function GET() {
try { try {
const session = await getServerSession(authOptions); const session = await getServerSession(authOptions);
const cookieStore = cookies();
if (!session?.user?.email) { if (!session?.user?.email) {
return NextResponse.json( return NextResponse.json(
@ -52,10 +17,7 @@ export async function GET() {
} }
const nextcloudUrl = process.env.NEXTCLOUD_URL; const nextcloudUrl = process.env.NEXTCLOUD_URL;
const nextcloudClientId = process.env.NEXTCLOUD_CLIENT_ID; if (!nextcloudUrl) {
const nextcloudClientSecret = process.env.NEXTCLOUD_CLIENT_SECRET;
if (!nextcloudUrl || !nextcloudClientId || !nextcloudClientSecret) {
console.error('Missing Nextcloud configuration'); console.error('Missing Nextcloud configuration');
return NextResponse.json( return NextResponse.json(
{ error: 'Nextcloud configuration is missing' }, { error: 'Nextcloud configuration is missing' },
@ -74,16 +36,18 @@ export async function GET() {
} }
try { try {
// Get access token // Get user's folders using WebDAV with session cookies
const accessToken = await getNextcloudAccessToken(nextcloudClientId, nextcloudClientSecret);
// Get user's folders using WebDAV
const webdavUrl = `${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(session.user.email)}/`; const webdavUrl = `${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(session.user.email)}/`;
console.log('Requesting WebDAV URL:', webdavUrl); console.log('Requesting WebDAV URL:', webdavUrl);
// Get all cookies from the request
const cookieHeader = cookieStore.getAll()
.map(cookie => `${cookie.name}=${cookie.value}`)
.join('; ');
const foldersResponse = await fetch(webdavUrl, { const foldersResponse = await fetch(webdavUrl, {
headers: { headers: {
'Authorization': `Bearer ${accessToken}`, 'Cookie': cookieHeader,
'Depth': '1', 'Depth': '1',
'Content-Type': 'application/xml', 'Content-Type': 'application/xml',
}, },