carnet api
This commit is contained in:
parent
32ac30631d
commit
ae5176e90a
@ -2,47 +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';
|
||||
|
||||
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;
|
||||
}
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
const cookieStore = cookies();
|
||||
|
||||
if (!session?.user?.email) {
|
||||
return NextResponse.json(
|
||||
@ -52,10 +17,7 @@ export async function GET() {
|
||||
}
|
||||
|
||||
const nextcloudUrl = process.env.NEXTCLOUD_URL;
|
||||
const nextcloudClientId = process.env.NEXTCLOUD_CLIENT_ID;
|
||||
const nextcloudClientSecret = process.env.NEXTCLOUD_CLIENT_SECRET;
|
||||
|
||||
if (!nextcloudUrl || !nextcloudClientId || !nextcloudClientSecret) {
|
||||
if (!nextcloudUrl) {
|
||||
console.error('Missing Nextcloud configuration');
|
||||
return NextResponse.json(
|
||||
{ error: 'Nextcloud configuration is missing' },
|
||||
@ -74,16 +36,18 @@ export async function GET() {
|
||||
}
|
||||
|
||||
try {
|
||||
// Get access token
|
||||
const accessToken = await getNextcloudAccessToken(nextcloudClientId, nextcloudClientSecret);
|
||||
|
||||
// Get user's folders using WebDAV
|
||||
// Get user's folders using WebDAV with session cookies
|
||||
const webdavUrl = `${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(session.user.email)}/`;
|
||||
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, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${accessToken}`,
|
||||
'Cookie': cookieHeader,
|
||||
'Depth': '1',
|
||||
'Content-Type': 'application/xml',
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user