diff --git a/.changeset/cuddly-suns-sit.md b/.changeset/cuddly-suns-sit.md new file mode 100644 index 0000000000..f1fe16add6 --- /dev/null +++ b/.changeset/cuddly-suns-sit.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-user-settings': patch +--- + +Fix undefined identity bug in UserSettingsProfileCard caused by using deprecated methods of the IdentityApi diff --git a/plugins/user-settings/src/components/useUserProfileInfo.ts b/plugins/user-settings/src/components/useUserProfileInfo.ts index 336ea34ee4..075dead332 100644 --- a/plugins/user-settings/src/components/useUserProfileInfo.ts +++ b/plugins/user-settings/src/components/useUserProfileInfo.ts @@ -14,13 +14,36 @@ * limitations under the License. */ -import { useApi, identityApiRef } from '@backstage/core-plugin-api'; +import { + useApi, + identityApiRef, + BackstageUserIdentity, + ProfileInfo, +} from '@backstage/core-plugin-api'; +import { useEffect, useState } from 'react'; export const useUserProfile = () => { const identityApi = useApi(identityApiRef); - const userId = identityApi.getUserId(); - const profile = identityApi.getProfile(); - const displayName = profile.displayName ?? userId; + + const [displayName, setDisplayName] = useState< + string | BackstageUserIdentity + >(''); + const [profile, setProfile] = useState({}); + + const getUserProfile = async () => { + const backstageIdentity = await identityApi.getBackstageIdentity(); + const profileInfo = await identityApi.getProfileInfo(); + const name = profileInfo.displayName ?? backstageIdentity; + + setDisplayName(name); + setProfile(profileInfo); + }; + + useEffect(() => { + if (!displayName) { + getUserProfile(); + } + }); return { profile, displayName }; };