Page logic

This commit is contained in:
alma 2025-04-21 11:28:55 +02:00
parent 8ab7ddc7b0
commit 2d142a9325
3 changed files with 80 additions and 1 deletions

View File

@ -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;

View File

@ -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 ...
}

View File

@ -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 (
<div
className="min-h-screen flex items-center justify-center"