fix: send token in keycloak function & change script fix retire

This commit is contained in:
Warunee Tamkoo 2025-10-20 23:04:04 +07:00
parent 0f22605784
commit 2429159020
2 changed files with 429 additions and 220 deletions

View file

@ -67,11 +67,16 @@ export async function getToken() {
*
* @returns user uuid or true if success, false otherwise.
*/
export async function createUser(username: string, password: string, opts?: Record<string, any>) {
export async function createUser(
username: string,
password: string,
opts?: Record<string, any>,
token?: string,
) {
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users`, {
// prettier-ignore
headers: {
"authorization": `Bearer ${await getToken()}`,
"authorization": `Bearer ${token || await getToken()}`,
"content-type": `application/json`,
},
method: "POST",
@ -101,11 +106,11 @@ export async function createUser(username: string, password: string, opts?: Reco
*
* @returns user if success, false otherwise.
*/
export async function getUser(userId: string) {
export async function getUser(userId: string, token?: string) {
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}`, {
// prettier-ignore
headers: {
"authorization": `Bearer ${await getToken()}`,
"authorization": `Bearer ${token || await getToken()}`,
"content-type": `application/json`,
},
}).catch((e) => console.log("Keycloak Error: ", e));
@ -123,11 +128,11 @@ export async function getUser(userId: string) {
*
* @returns user if success, false otherwise.
*/
export async function getUserByUsername(citizenId: string) {
export async function getUserByUsername(citizenId: string, token?: string) {
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users?username=${citizenId}`, {
// prettier-ignore
headers: {
"authorization": `Bearer ${await getToken()}`,
"authorization": `Bearer ${token || await getToken()}`,
"content-type": `application/json`,
},
}).catch((e) => console.log("Keycloak Error: ", e));
@ -409,13 +414,13 @@ export async function getRoleMappings(userId: string) {
*
* @returns role's info (array if not specify name) if success, null if not found, false otherwise.
*/
export async function getRoles(name?: string) {
export async function getRoles(name?: string, token?: string) {
const res = await fetch(
`${KC_URL}/admin/realms/${KC_REALMS}/roles`.concat((name && `/${name}`) || ""),
{
// prettier-ignore
headers: {
"authorization": `Bearer ${await getToken()}`,
"authorization": `Bearer ${token || await getToken()}`,
},
},
).catch((e) => console.log(e));
@ -491,13 +496,17 @@ export async function getUserRoles(userId: string) {
*
* @returns true if success, false otherwise.
*/
export async function addUserRoles(userId: string, roles: { id: string; name: string }[]) {
export async function addUserRoles(
userId: string,
roles: { id: string; name: string }[],
token?: string,
) {
const res = await fetch(
`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/role-mappings/realm`,
{
// prettier-ignore
headers: {
"authorization": `Bearer ${await getToken()}`,
"authorization": `Bearer ${token || await getToken()}`,
"content-type": `application/json`,
},
method: "POST",