update api users and groups2
This commit is contained in:
parent
833f73a766
commit
2d2aef29b4
@ -5,7 +5,7 @@ import { NextResponse } from "next/server";
|
|||||||
//TODO: Ajouter la suppression automatique du compte Nextcloud
|
//TODO: Ajouter la suppression automatique du compte Nextcloud
|
||||||
export async function DELETE(
|
export async function DELETE(
|
||||||
req: Request,
|
req: Request,
|
||||||
{ params }: { params: { id: string } }
|
{ params }: { params: { userId: string } }
|
||||||
) {
|
) {
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ export async function DELETE(
|
|||||||
|
|
||||||
// Delete user using admin token
|
// Delete user using admin token
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/users/${params.id}`,
|
`${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/users/${params.userId}`,
|
||||||
{
|
{
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
import { getServerSession } from "next-auth/next";
|
import { getServerSession } from "next-auth";
|
||||||
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
import { authOptions } from "../../auth/[...nextauth]/route";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
//TODO: Ajouter la suppression automatique du compte Nextcloud
|
||||||
export async function DELETE(
|
export async function DELETE(
|
||||||
req: Request,
|
req: Request,
|
||||||
{ params }: { params: { userId: string } }
|
{ params }: { params: { userId: string } }
|
||||||
@ -13,7 +14,7 @@ export async function DELETE(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get client credentials token
|
// First get an admin token using client credentials
|
||||||
const tokenResponse = await fetch(
|
const tokenResponse = await fetch(
|
||||||
`${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/token`,
|
`${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/token`,
|
||||||
{
|
{
|
||||||
@ -32,31 +33,45 @@ export async function DELETE(
|
|||||||
const tokenData = await tokenResponse.json();
|
const tokenData = await tokenResponse.json();
|
||||||
|
|
||||||
if (!tokenResponse.ok) {
|
if (!tokenResponse.ok) {
|
||||||
console.error("Failed to get token:", tokenData);
|
console.error("Failed to get admin token:", tokenData);
|
||||||
return NextResponse.json({ error: "Failed to get token" }, { status: 500 });
|
return NextResponse.json(
|
||||||
|
{ error: "Erreur d'authentification" },
|
||||||
|
{ status: 401 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete user
|
// Delete user using admin token
|
||||||
const deleteResponse = await fetch(
|
const response = await fetch(
|
||||||
`${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/users/${params.userId}`,
|
`${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/users/${params.userId}`,
|
||||||
{
|
{
|
||||||
method: 'DELETE',
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${tokenData.access_token}`,
|
Authorization: `Bearer ${tokenData.access_token}`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!deleteResponse.ok) {
|
console.log("Delete response:", {
|
||||||
const errorData = await deleteResponse.json();
|
status: response.status,
|
||||||
console.error("Failed to delete user:", errorData);
|
ok: response.ok
|
||||||
return NextResponse.json({ error: "Failed to delete user" }, { status: deleteResponse.status });
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorText = await response.text();
|
||||||
|
console.error("Delete error:", errorText);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Erreur lors de la suppression", details: errorText },
|
||||||
|
{ status: response.status }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json({ success: true });
|
return NextResponse.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error in DELETE user:", error);
|
console.error("Error deleting user:", error);
|
||||||
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
|
return NextResponse.json(
|
||||||
|
{ error: "Erreur serveur", details: error },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user