60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import { keycloakAdminHttp } from './httpClient';
|
|
import { KeycloakUserSummary } from './types';
|
|
|
|
export async function findUserByEmail(email: string): Promise<KeycloakUserSummary | null> {
|
|
const response = await keycloakAdminHttp.get<Array<{ id: string }>>('/users', {
|
|
params: { email, exact: true },
|
|
});
|
|
const [utente] = response.data;
|
|
return utente ? { id: utente.id } : null;
|
|
}
|
|
|
|
export interface KeycloakUserProfile {
|
|
id: string;
|
|
email: string;
|
|
firstName: string;
|
|
lastName: string;
|
|
}
|
|
|
|
export async function getUserById(userId: string): Promise<KeycloakUserProfile> {
|
|
const response = await keycloakAdminHttp.get<{
|
|
id: string;
|
|
email?: string;
|
|
firstName?: string;
|
|
lastName?: string;
|
|
}>(`/users/${userId}`);
|
|
|
|
return {
|
|
id: response.data.id,
|
|
email: response.data.email ?? '',
|
|
firstName: response.data.firstName ?? '',
|
|
lastName: response.data.lastName ?? '',
|
|
};
|
|
}
|
|
|
|
export interface UpdateUserProfileInput {
|
|
email: string;
|
|
firstName: string;
|
|
lastName: string;
|
|
}
|
|
|
|
// Il realm ha `registrationEmailAsUsername: true`, quindi username ed email
|
|
// devono restare sincronizzati: aggiornare l'email senza lo username
|
|
// lascerebbe l'utente con un login (username) diverso dalla nuova email.
|
|
export async function updateUserProfile(userId: string, input: UpdateUserProfileInput): Promise<void> {
|
|
await keycloakAdminHttp.put(`/users/${userId}`, {
|
|
email: input.email,
|
|
username: input.email,
|
|
firstName: input.firstName,
|
|
lastName: input.lastName,
|
|
});
|
|
}
|
|
|
|
export async function resetUserPassword(userId: string, password: string): Promise<void> {
|
|
await keycloakAdminHttp.put(`/users/${userId}/reset-password`, {
|
|
type: 'password',
|
|
value: password,
|
|
temporary: false,
|
|
});
|
|
}
|