equipes keycloak flow
This commit is contained in:
parent
2663d6f23a
commit
d2a1d119f4
@ -13,12 +13,16 @@ export async function GET(
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Safely access the userId parameter
|
||||
const userId = String(params?.userId || '');
|
||||
// Handle params correctly for Next.js App Router
|
||||
// Convert to string primitive to avoid "used `params.userId`" error
|
||||
const userIdParam = params.userId;
|
||||
const userId = String(userIdParam);
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: "User ID is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const kcAdminClient = await getKeycloakAdminClient();
|
||||
|
||||
// Get all available roles
|
||||
@ -33,6 +37,13 @@ export async function GET(
|
||||
availableRoles,
|
||||
userRoles,
|
||||
});
|
||||
} catch (keycloakError) {
|
||||
console.error("Error connecting to Keycloak:", keycloakError);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to connect to Keycloak service", details: String(keycloakError) },
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error fetching roles:", error);
|
||||
return NextResponse.json(
|
||||
@ -52,12 +63,16 @@ export async function PUT(
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Safely access the userId parameter
|
||||
const userId = String(params?.userId || '');
|
||||
// Handle params correctly for Next.js App Router
|
||||
// Convert to string primitive to avoid "used `params.userId`" error
|
||||
const userIdParam = params.userId;
|
||||
const userId = String(userIdParam);
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: "User ID is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const { roles } = await request.json();
|
||||
const kcAdminClient = await getKeycloakAdminClient();
|
||||
|
||||
@ -97,6 +112,13 @@ export async function PUT(
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (keycloakError) {
|
||||
console.error("Error connecting to Keycloak:", keycloakError);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to connect to Keycloak service", details: String(keycloakError) },
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error updating roles:", error);
|
||||
return NextResponse.json(
|
||||
|
||||
@ -21,11 +21,21 @@ export async function getKeycloakAdminClient(): Promise<KcAdminClient> {
|
||||
}
|
||||
}
|
||||
|
||||
const keycloakUrl = process.env.KEYCLOAK_BASE_URL || process.env.NEXT_PUBLIC_KEYCLOAK_ISSUER || 'http://localhost:8080';
|
||||
const adminClientId = process.env.KEYCLOAK_ADMIN_CLIENT_ID || 'admin-cli';
|
||||
const adminUsername = process.env.KEYCLOAK_ADMIN_USERNAME || 'admin';
|
||||
const adminPassword = process.env.KEYCLOAK_ADMIN_PASSWORD || 'admin';
|
||||
const realmName = process.env.KEYCLOAK_REALM || 'cercle';
|
||||
// Only use environment variables - no hardcoded defaults
|
||||
const keycloakUrl = process.env.KEYCLOAK_BASE_URL || process.env.KEYCLOAK_ISSUER || process.env.NEXT_PUBLIC_KEYCLOAK_ISSUER;
|
||||
const adminClientId = process.env.KEYCLOAK_ADMIN_CLIENT_ID;
|
||||
const adminUsername = process.env.KEYCLOAK_ADMIN_USERNAME;
|
||||
const adminPassword = process.env.KEYCLOAK_ADMIN_PASSWORD;
|
||||
const realmName = process.env.KEYCLOAK_REALM;
|
||||
|
||||
// Validate required environment variables
|
||||
if (!keycloakUrl) {
|
||||
throw new Error('Missing Keycloak URL. Please set KEYCLOAK_BASE_URL or KEYCLOAK_ISSUER or NEXT_PUBLIC_KEYCLOAK_ISSUER in your environment variables.');
|
||||
}
|
||||
|
||||
if (!adminClientId || !adminUsername || !adminPassword || !realmName) {
|
||||
throw new Error('Missing Keycloak admin credentials. Please set KEYCLOAK_ADMIN_CLIENT_ID, KEYCLOAK_ADMIN_USERNAME, KEYCLOAK_ADMIN_PASSWORD, and KEYCLOAK_REALM in your environment variables.');
|
||||
}
|
||||
|
||||
console.log(`Connecting to Keycloak at ${keycloakUrl}, realm: ${realmName}`);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user