update api users and groups3

This commit is contained in:
Alma 2025-04-09 20:15:48 +02:00
parent 2d2aef29b4
commit e46e745c5c
2 changed files with 66 additions and 3 deletions

View File

@ -0,0 +1,63 @@
import { getServerSession } from "next-auth/next";
import { authOptions } from "../../auth/[...nextauth]/route";
import { NextResponse } from "next/server";
async function getAdminToken() {
const tokenResponse = await fetch(
`${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/token`,
{
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
grant_type: 'client_credentials',
client_id: process.env.KEYCLOAK_CLIENT_ID!,
client_secret: process.env.KEYCLOAK_CLIENT_SECRET!,
}),
}
);
const data = await tokenResponse.json();
if (!tokenResponse.ok) {
throw new Error(data.error_description || 'Failed to get admin token');
}
return data.access_token;
}
export async function DELETE(
req: Request,
{ params }: { params: { groupId: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session) {
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
}
const token = await getAdminToken();
const response = await fetch(
`${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/groups/${params.groupId}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${token}`,
},
}
);
if (!response.ok) {
throw new Error('Failed to delete group');
}
return NextResponse.json({ success: true });
} catch (error) {
console.error('Delete Group Error:', error);
return NextResponse.json(
{ error: "Erreur lors de la suppression du groupe" },
{ status: 500 }
);
}
}

View File

@ -28,7 +28,7 @@ async function getAdminToken() {
export async function DELETE(
req: Request,
{ params }: { params: { id: string } }
{ params }: { params: { groupId: string } }
) {
try {
const session = await getServerSession(authOptions);
@ -39,7 +39,7 @@ export async function DELETE(
const token = await getAdminToken();
const response = await fetch(
`${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/groups/${params.id}`,
`${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/groups/${params.groupId}`,
{
method: "DELETE",
headers: {