refactor: user relation
This commit is contained in:
parent
a74d8b63b1
commit
2bd30b735d
21 changed files with 607 additions and 185 deletions
|
|
@ -59,6 +59,61 @@ export async function getToken() {
|
|||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get keycloak user list
|
||||
*
|
||||
* @returns user list if success, false otherwise.
|
||||
*/
|
||||
export async function listUser(search = "", page = 1, pageSize = 30) {
|
||||
const res = await fetch(
|
||||
`${KC_URL}/admin/realms/${KC_REALM}/users?first=${(page - 1) * pageSize}&max=${pageSize}`.concat(
|
||||
!!search ? `&search=${search}` : "",
|
||||
),
|
||||
{
|
||||
headers: {
|
||||
authorization: `Bearer ${await getToken()}`,
|
||||
"content-type": `application/json`,
|
||||
},
|
||||
},
|
||||
).catch((e) => console.log("Keycloak Error: ", e));
|
||||
|
||||
if (!res) return;
|
||||
if (!res.ok) {
|
||||
return console.error("Keycloak Error Response: ", await res.json());
|
||||
}
|
||||
return ((await res.json()) as any[]).map((v: Record<string, string>) => ({
|
||||
id: v.id,
|
||||
username: v.username,
|
||||
firstName: v.firstName,
|
||||
lastName: v.lastName,
|
||||
email: v.email,
|
||||
attributes: v.attributes,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Count user in the system. Can be use for pagination purpose.
|
||||
*
|
||||
* @returns numer of user on success.
|
||||
*/
|
||||
export async function countUser(search = "") {
|
||||
const res = await fetch(
|
||||
`${KC_URL}/admin/realms/${KC_REALM}/users/count`.concat(!!search ? `?search=${search}` : ""),
|
||||
{
|
||||
headers: {
|
||||
authorization: `Bearer ${await getToken()}`,
|
||||
"content-type": `application/json`,
|
||||
},
|
||||
},
|
||||
).catch((e) => console.log("Keycloak Error: ", e));
|
||||
|
||||
if (!res) return;
|
||||
if (!res.ok) {
|
||||
return console.error("Keycloak Error Response: ", await res.json());
|
||||
}
|
||||
return (await res.json()) as number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create keycloak user by given username and password with roles
|
||||
*
|
||||
|
|
@ -151,36 +206,42 @@ export async function deleteUser(userId: string) {
|
|||
|
||||
/**
|
||||
* Get roles list or specific role data
|
||||
*
|
||||
* Client must have permission to get realms roles
|
||||
*
|
||||
* @returns role's info (array if not specify name) if success, null if not found, false otherwise.
|
||||
*/
|
||||
export async function getRoles(name?: string) {
|
||||
const res = await fetch(
|
||||
`${KC_URL}/admin/realms/${KC_REALM}/roles`.concat((name && `/${name}`) || ""),
|
||||
{
|
||||
// prettier-ignore
|
||||
headers: {
|
||||
"authorization": `Bearer ${await getToken()}`,
|
||||
export async function listRole() {
|
||||
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/roles`, {
|
||||
headers: {
|
||||
authorization: `Bearer ${await getToken()}`,
|
||||
},
|
||||
},
|
||||
).catch((e) => console.log(e));
|
||||
}).catch((e) => console.log(e));
|
||||
|
||||
if (!res) return false;
|
||||
if (!res) return;
|
||||
if (!res.ok && res.status !== 404) {
|
||||
return Boolean(console.error("Keycloak Error Response: ", await res.json()));
|
||||
return console.error("Keycloak Error Response: ", await res.json());
|
||||
}
|
||||
|
||||
if (res.status === 404) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
const data = (await res.json()) as any[];
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
return data.map((v: Record<string, string>) => ({ id: v.id, name: v.name }));
|
||||
return data.map((v: Record<string, string>) => ({ id: v.id, name: v.name }));
|
||||
}
|
||||
|
||||
export async function getRoleByName(name: string) {
|
||||
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/roles`.concat(`/${name}`), {
|
||||
headers: {
|
||||
authorization: `Bearer ${await getToken()}`,
|
||||
},
|
||||
}).catch((e) => console.log(e));
|
||||
|
||||
if (!res) return;
|
||||
if (!res.ok && res.status !== 404) {
|
||||
return console.error("Keycloak Error Response: ", await res.json());
|
||||
}
|
||||
if (res.status === 404) return null;
|
||||
|
||||
const data = (await res.json()) as any;
|
||||
|
||||
return {
|
||||
id: data.id,
|
||||
|
|
@ -285,7 +346,7 @@ export async function removeUserRoles(userId: string, roles: { id: string; name:
|
|||
|
||||
export default {
|
||||
createUser,
|
||||
getRoles,
|
||||
listRole,
|
||||
addUserRoles,
|
||||
removeUserRoles,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue