0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

Merge pull request #1043 from logto-io/charles-log-2815-replace-user-api-with-oidc-me

refactor(console): replace user api with fetchUserInfo
This commit is contained in:
Charles Zhao 2022-06-06 10:19:31 +08:00 committed by GitHub
commit 79ea8b2c68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 297 additions and 135 deletions

View file

@ -19,7 +19,7 @@
"devDependencies": {
"@fontsource/roboto-mono": "^4.5.7",
"@logto/phrases": "^0.1.0",
"@logto/react": "^0.1.13",
"@logto/react": "^0.1.14",
"@logto/shared": "^0.1.0",
"@logto/schemas": "^0.1.0",
"@mdx-js/react": "^1.6.22",

View file

@ -1,5 +1,5 @@
import { useLogto } from '@logto/react';
import { User, UserRole } from '@logto/schemas';
import { useLogto, UserInfoResponse } from '@logto/react';
import { UserRole } from '@logto/schemas';
import classNames from 'classnames';
import React, { useEffect, useRef, useState, MouseEvent } from 'react';
import { useTranslation } from 'react-i18next';
@ -13,33 +13,29 @@ import SignOut from '@/icons/SignOut';
import * as styles from './index.module.scss';
const UserInfo = () => {
const { isAuthenticated, getIdTokenClaims, signOut } = useLogto();
const { isAuthenticated, fetchUserInfo, signOut } = useLogto();
const api = useApi();
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const anchorRef = useRef<HTMLDivElement>(null);
const [showDropDown, setShowDropdown] = useState(false);
const [user, setUser] = useState<User>();
const [user, setUser] = useState<UserInfoResponse>();
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
(async () => {
if (isAuthenticated) {
const { sub: userId } = getIdTokenClaims() ?? {};
if (userId) {
const data = await api.get(`/api/users/${userId}`).json<User>();
setUser(data);
}
const userInfo = await fetchUserInfo();
setUser(userInfo);
}
})();
}, [api, isAuthenticated, getIdTokenClaims]);
}, [api, isAuthenticated, fetchUserInfo]);
if (!user) {
return null;
}
const { id, name, avatar, roleNames } = user;
const isAdmin = roleNames.includes(UserRole.Admin);
const { sub: id, name, avatar, role_names: roleNames } = user;
const isAdmin = roleNames?.includes(UserRole.Admin);
return (
<>

View file

@ -17,7 +17,7 @@
"stylelint": "stylelint \"src/**/*.scss\""
},
"devDependencies": {
"@logto/react": "^0.1.13",
"@logto/react": "^0.1.14",
"@logto/schemas": "^0.1.0",
"@logto/shared": "^0.1.0",
"@parcel/core": "2.5.0",

File diff suppressed because it is too large Load diff