Page logic
This commit is contained in:
parent
8ab7ddc7b0
commit
2d142a9325
@ -33,6 +33,7 @@ declare module "next-auth" {
|
|||||||
first_name: string;
|
first_name: string;
|
||||||
last_name: string;
|
last_name: string;
|
||||||
role: string[];
|
role: string[];
|
||||||
|
nextcloudInitialized?: boolean;
|
||||||
};
|
};
|
||||||
accessToken: string;
|
accessToken: string;
|
||||||
}
|
}
|
||||||
@ -193,6 +194,7 @@ export const authOptions: NextAuthOptions = {
|
|||||||
first_name: token.first_name ?? '',
|
first_name: token.first_name ?? '',
|
||||||
last_name: token.last_name ?? '',
|
last_name: token.last_name ?? '',
|
||||||
role: userRoles,
|
role: userRoles,
|
||||||
|
nextcloudInitialized: false,
|
||||||
};
|
};
|
||||||
session.accessToken = token.accessToken;
|
session.accessToken = token.accessToken;
|
||||||
|
|
||||||
|
|||||||
60
app/api/nextcloud/init/route.ts
Normal file
60
app/api/nextcloud/init/route.ts
Normal 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 ...
|
||||||
|
}
|
||||||
@ -1,14 +1,31 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { signIn } from "next-auth/react";
|
import { signIn, useSession } from "next-auth/react";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
|
||||||
export default function SignIn() {
|
export default function SignIn() {
|
||||||
|
const { data: session } = useSession();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Trigger Keycloak sign-in
|
// Trigger Keycloak sign-in
|
||||||
signIn("keycloak", { callbackUrl: "/" });
|
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 (
|
return (
|
||||||
<div
|
<div
|
||||||
className="min-h-screen flex items-center justify-center"
|
className="min-h-screen flex items-center justify-center"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user