mirror of
https://github.com/logto-io/logto.git
synced 2025-03-03 22:15:32 -05:00
refactor(console): fix user data reloading issue
This commit is contained in:
parent
9f9446ecda
commit
aaa9b47781
5 changed files with 17 additions and 41 deletions
packages/console/src
|
@ -1,3 +1,4 @@
|
||||||
|
import { useLogto } from '@logto/react';
|
||||||
import type { UserProfileResponse } from '@logto/schemas';
|
import type { UserProfileResponse } from '@logto/schemas';
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
|
|
||||||
|
@ -5,18 +6,17 @@ import { adminTenantEndpoint, meApi } from '@/consts';
|
||||||
|
|
||||||
import type { RequestError } from './use-api';
|
import type { RequestError } from './use-api';
|
||||||
import { useStaticApi } from './use-api';
|
import { useStaticApi } from './use-api';
|
||||||
import useLogtoUserId from './use-logto-user-id';
|
|
||||||
import useSwrFetcher from './use-swr-fetcher';
|
import useSwrFetcher from './use-swr-fetcher';
|
||||||
|
|
||||||
const useCurrentUser = () => {
|
const useCurrentUser = () => {
|
||||||
const userId = useLogtoUserId();
|
const { isAuthenticated } = useLogto();
|
||||||
const api = useStaticApi({ prefixUrl: adminTenantEndpoint, resourceIndicator: meApi.indicator });
|
const api = useStaticApi({ prefixUrl: adminTenantEndpoint, resourceIndicator: meApi.indicator });
|
||||||
const fetcher = useSwrFetcher<UserProfileResponse>(api);
|
const fetcher = useSwrFetcher<UserProfileResponse>(api);
|
||||||
const {
|
const {
|
||||||
data: user,
|
data: user,
|
||||||
error,
|
error,
|
||||||
mutate,
|
mutate,
|
||||||
} = useSWR<UserProfileResponse, RequestError>(userId && 'me', fetcher);
|
} = useSWR<UserProfileResponse, RequestError>(isAuthenticated && 'me', fetcher);
|
||||||
|
|
||||||
const isLoading = !user && !error;
|
const isLoading = !user && !error;
|
||||||
|
|
||||||
|
|
|
@ -1,25 +0,0 @@
|
||||||
import { useLogto } from '@logto/react';
|
|
||||||
import { useEffect, useState } from 'react';
|
|
||||||
|
|
||||||
const useLogtoUserId = () => {
|
|
||||||
const { getIdTokenClaims, isAuthenticated } = useLogto();
|
|
||||||
const [userId, setUserId] = useState<string>();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const fetch = async () => {
|
|
||||||
const claims = await getIdTokenClaims();
|
|
||||||
setUserId(claims?.sub);
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isAuthenticated) {
|
|
||||||
void fetch();
|
|
||||||
} else {
|
|
||||||
// eslint-disable-next-line unicorn/no-useless-undefined
|
|
||||||
setUserId(undefined);
|
|
||||||
}
|
|
||||||
}, [getIdTokenClaims, isAuthenticated]);
|
|
||||||
|
|
||||||
return userId;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default useLogtoUserId;
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { type JsonObject } from '@logto/schemas';
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { toast } from 'react-hot-toast';
|
import { toast } from 'react-hot-toast';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
@ -9,19 +10,20 @@ const useMeCustomData = () => {
|
||||||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
|
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
|
||||||
|
|
||||||
const update = useCallback(
|
const update = useCallback(
|
||||||
async (customData: Record<string, unknown>) => {
|
async (customData: JsonObject) => {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
toast.error(t('errors.unexpected_error'));
|
toast.error(t('errors.unexpected_error'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const updated = await api
|
await reload({
|
||||||
.patch(`me/custom-data`, {
|
...user,
|
||||||
json: customData,
|
customData: await api
|
||||||
})
|
.patch(`me/custom-data`, {
|
||||||
.json<(typeof user)['customData']>();
|
json: customData,
|
||||||
|
})
|
||||||
await reload({ ...user, customData: updated });
|
.json<JsonObject>(),
|
||||||
|
});
|
||||||
},
|
},
|
||||||
[api, reload, t, user]
|
[api, reload, t, user]
|
||||||
);
|
);
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { getCallbackUrl } from '@/consts';
|
||||||
import { TenantsContext } from '@/contexts/TenantsProvider';
|
import { TenantsContext } from '@/contexts/TenantsProvider';
|
||||||
|
|
||||||
const useValidateTenantAccess = () => {
|
const useValidateTenantAccess = () => {
|
||||||
const { getAccessToken, signIn, isAuthenticated } = useLogto();
|
const { getAccessToken, signIn } = useLogto();
|
||||||
const { currentTenant, currentTenantId, currentTenantValidated, setCurrentTenantValidated } =
|
const { currentTenant, currentTenantId, currentTenantValidated, setCurrentTenantValidated } =
|
||||||
useContext(TenantsContext);
|
useContext(TenantsContext);
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ const useValidateTenantAccess = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isAuthenticated && currentTenantId && !currentTenantValidated) {
|
if (currentTenantId && !currentTenantValidated) {
|
||||||
setCurrentTenantValidated();
|
setCurrentTenantValidated();
|
||||||
if (currentTenant) {
|
if (currentTenant) {
|
||||||
void validate(currentTenant);
|
void validate(currentTenant);
|
||||||
|
@ -37,7 +37,6 @@ const useValidateTenantAccess = () => {
|
||||||
currentTenantId,
|
currentTenantId,
|
||||||
currentTenantValidated,
|
currentTenantValidated,
|
||||||
getAccessToken,
|
getAccessToken,
|
||||||
isAuthenticated,
|
|
||||||
setCurrentTenantValidated,
|
setCurrentTenantValidated,
|
||||||
signIn,
|
signIn,
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -103,9 +103,9 @@ export function OnboardingRoutes() {
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route element={<ProtectedRoutes />}>
|
<Route element={<ProtectedRoutes />}>
|
||||||
<Route element={<Layout />}>
|
<Route element={<Layout />}>
|
||||||
<Route index element={<Navigate replace to={welcomePathname} />} />
|
<Route path="/" element={<Navigate replace to={welcomePathname} />} />
|
||||||
<Route path={`/${OnboardingRoute.Onboarding}`} element={<AppContent />}>
|
<Route path={`/${OnboardingRoute.Onboarding}`} element={<AppContent />}>
|
||||||
<Route index element={<Navigate replace to={welcomePathname} />} />
|
<Route path="" element={<Navigate replace to={welcomePathname} />} />
|
||||||
<Route path={OnboardingPage.Welcome} element={<Welcome />} />
|
<Route path={OnboardingPage.Welcome} element={<Welcome />} />
|
||||||
<Route path={OnboardingPage.AboutUser} element={<About />} />
|
<Route path={OnboardingPage.AboutUser} element={<About />} />
|
||||||
<Route path={OnboardingPage.SignInExperience} element={<SignInExperience />} />
|
<Route path={OnboardingPage.SignInExperience} element={<SignInExperience />} />
|
||||||
|
|
Loading…
Add table
Reference in a new issue