keycloak-scim/apps/account-ui/src/api.ts
Erik Jan de Wit b7e4db8e15
Add 'Personal Info' form and masthead to Account UI (#3551)
Co-authored-by: Jon Koops <jonkoops@gmail.com>
2022-11-03 15:06:26 +01:00

37 lines
844 B
TypeScript

import { environment } from "./environment";
import { keycloak } from "./keycloak";
import { UserRepresentation } from "./representations";
import { joinPath } from "./utils/joinPath";
export const fetchPersonalInfo = (params: RequestInit) =>
get<UserRepresentation>("/", params);
async function get<T>(path: string, params: RequestInit): Promise<T> {
const url = joinPath(
environment.authServerUrl,
"realms",
environment.loginRealm,
"account",
path
);
const response = await fetch(url, {
...params,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${await getAccessToken()}`,
},
});
return response.json();
}
async function getAccessToken() {
try {
await keycloak.updateToken(5);
} catch (error) {
keycloak.login();
}
return keycloak.token;
}