From 2d142a9325aa5c535badf7ef2c4752db895bc5d8 Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 21 Apr 2025 11:28:55 +0200 Subject: [PATCH] Page logic --- app/api/auth/[...nextauth]/route.ts | 2 + app/api/nextcloud/init/route.ts | 60 +++++++++++++++++++++++++++++ app/signin/page.tsx | 19 ++++++++- 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 app/api/nextcloud/init/route.ts diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index 7e5bb688..df61d656 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -33,6 +33,7 @@ declare module "next-auth" { first_name: string; last_name: string; role: string[]; + nextcloudInitialized?: boolean; }; accessToken: string; } @@ -193,6 +194,7 @@ export const authOptions: NextAuthOptions = { first_name: token.first_name ?? '', last_name: token.last_name ?? '', role: userRoles, + nextcloudInitialized: false, }; session.accessToken = token.accessToken; diff --git a/app/api/nextcloud/init/route.ts b/app/api/nextcloud/init/route.ts new file mode 100644 index 00000000..4cf191a1 --- /dev/null +++ b/app/api/nextcloud/init/route.ts @@ -0,0 +1,60 @@ +import { NextResponse } from 'next/server'; +import { getServerSession } from 'next-auth'; +import { authOptions } from '@/app/api/auth/[...nextauth]/route'; +import { prisma } from '@/lib/prisma'; +import { Buffer } from 'buffer'; + +export async function POST() { + try { + const session = await getServerSession(authOptions); + if (!session?.user) { + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + } + + const nextcloudUrl = process.env.NEXTCLOUD_URL; + const adminUsername = process.env.NEXTCLOUD_ADMIN_USERNAME; + const adminPassword = process.env.NEXTCLOUD_ADMIN_PASSWORD; + + if (!nextcloudUrl || !adminUsername || !adminPassword) { + return NextResponse.json({ error: 'Nextcloud configuration missing' }, { status: 500 }); + } + + const username = `cube-${session.user.id}`; + const userId = session.user.id; + + // Get or create WebDAV credentials + const password = await getWebDAVCredentials(nextcloudUrl, username, adminUsername, adminPassword, userId); + + // Ensure folder structure exists + await ensureFolderStructure(nextcloudUrl, username, password); + + // Store initialization state in session + const updatedSession = { + ...session, + user: { + ...session.user, + nextcloudInitialized: true + } + }; + + return NextResponse.json({ + success: true, + message: 'Nextcloud initialized successfully' + }); + } catch (error) { + console.error('Nextcloud initialization failed:', error); + return NextResponse.json({ + error: 'Failed to initialize Nextcloud', + details: error instanceof Error ? error.message : 'Unknown error' + }, { status: 500 }); + } +} + +// Helper functions from status/route.ts +async function getWebDAVCredentials(nextcloudUrl: string, username: string, adminUsername: string, adminPassword: string, userId: string) { + // ... existing getWebDAVCredentials implementation ... +} + +async function ensureFolderStructure(nextcloudUrl: string, username: string, password: string) { + // ... existing ensureFolderStructure implementation ... +} \ No newline at end of file diff --git a/app/signin/page.tsx b/app/signin/page.tsx index f5ff9c18..9a947eb9 100644 --- a/app/signin/page.tsx +++ b/app/signin/page.tsx @@ -1,14 +1,31 @@ "use client"; -import { signIn } from "next-auth/react"; +import { signIn, useSession } from "next-auth/react"; import { useEffect } from "react"; export default function SignIn() { + const { data: session } = useSession(); + useEffect(() => { // Trigger Keycloak sign-in signIn("keycloak", { callbackUrl: "/" }); }, []); + useEffect(() => { + if (session?.user && !session.user.nextcloudInitialized) { + // Initialize Nextcloud + fetch('/api/nextcloud/init', { + method: 'POST' + }).then(response => { + if (!response.ok) { + console.error('Failed to initialize Nextcloud'); + } + }).catch(error => { + console.error('Error initializing Nextcloud:', error); + }); + } + }, [session]); + return (