diff --git a/app/api/nextcloud/status/route.ts b/app/api/nextcloud/status/route.ts index 0182faed..b1fb24fd 100644 --- a/app/api/nextcloud/status/route.ts +++ b/app/api/nextcloud/status/route.ts @@ -140,33 +140,6 @@ async function ensureFolderStructure(nextcloudUrl: string, username: string, pas async function getWebDAVCredentials(nextcloudUrl: string, username: string, adminUsername: string, adminPassword: string, userId: string) { try { - // Check cache first - const cachedCredentials = credentialsCache.get(userId); - if (cachedCredentials) { - const cacheAge = Date.now() - cachedCredentials.timestamp; - if (cacheAge < 5 * 60 * 1000) { // 5 minutes cache - // Verify the cached credentials still work - const verifyResponse = await fetch(`${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(username)}/`, { - method: 'PROPFIND', - headers: { - 'Authorization': `Basic ${Buffer.from(`${username}:${cachedCredentials.password}`).toString('base64')}`, - 'Depth': '1', - 'Content-Type': 'application/xml', - }, - body: '', - }); - - if (verifyResponse.ok) { - console.log('Using cached credentials'); - return cachedCredentials.password; - } - - // If verification failed, remove from cache - console.log('Cached credentials verification failed'); - credentialsCache.delete(userId); - } - } - // First check if user exists in Nextcloud const userInfoResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, { headers: { @@ -184,27 +157,45 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi throw new Error(`Failed to get user info: ${userInfoResponse.status} ${userInfoResponse.statusText}`); } - // Try to use the admin password first - const adminVerifyResponse = await fetch(`${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(username)}/`, { - method: 'PROPFIND', - headers: { - 'Authorization': `Basic ${Buffer.from(`${username}:${adminPassword}`).toString('base64')}`, - 'Depth': '1', - 'Content-Type': 'application/xml', - }, - body: '', + // Check database for existing credentials + const existingCredentials = await prisma.webDAVCredentials.findUnique({ + where: { userId } }); - if (adminVerifyResponse.ok) { - console.log('Using admin password for WebDAV access'); - return adminPassword; + if (existingCredentials) { + // Verify if the existing credentials still work + const verifyResponse = await fetch(`${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(username)}/`, { + method: 'PROPFIND', + headers: { + 'Authorization': `Basic ${Buffer.from(`${username}:${existingCredentials.password}`).toString('base64')}`, + 'Depth': '1', + 'Content-Type': 'application/xml', + }, + body: '', + }); + + if (verifyResponse.ok) { + console.log('Using existing credentials from database'); + // Update cache + credentialsCache.set(userId, { + password: existingCredentials.password, + timestamp: Date.now() + }); + return existingCredentials.password; + } + + // If verification failed, delete the invalid credentials + console.log('Existing credentials verification failed, removing from database'); + await prisma.webDAVCredentials.delete({ + where: { userId } + }); } - // If admin password doesn't work, generate a new password + // If we get here, we need to generate a new password const newPassword = Math.random().toString(36).slice(-12); console.log('Setting new password for user'); - // Set the user's password + // Set the user's password in Nextcloud const setPasswordResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, { method: 'PUT', headers: { @@ -222,15 +213,26 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi throw new Error(`Failed to set password: ${setPasswordResponse.status} ${setPasswordResponse.statusText}`); } + // Store the new credentials in the database + await prisma.webDAVCredentials.upsert({ + where: { userId }, + update: { + username: username, + password: newPassword + }, + create: { + userId, + username: username, + password: newPassword + } + }); + // Update cache credentialsCache.set(userId, { password: newPassword, timestamp: Date.now() }); - // Try to create folders, but don't fail if it doesn't work - await ensureFolderStructure(nextcloudUrl, username, newPassword); - return newPassword; } catch (error) { console.error('Error in getWebDAVCredentials:', error);