0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-27 21:39:16 -05:00

refactor(console): fix me api error

This commit is contained in:
Gao Sun 2022-09-24 15:55:13 +08:00
parent bf1d281905
commit c86155131a
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
3 changed files with 46 additions and 3 deletions

View file

@ -0,0 +1,25 @@
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;

View file

@ -2,13 +2,16 @@ import { languageKeys } from '@logto/core-kit';
import { useLogto } from '@logto/react'; import { useLogto } from '@logto/react';
import { AppearanceMode } from '@logto/schemas'; import { AppearanceMode } from '@logto/schemas';
import { Nullable, Optional } from '@silverhand/essentials'; import { Nullable, Optional } from '@silverhand/essentials';
import { t } from 'i18next';
import { useCallback, useEffect, useMemo } from 'react'; import { useCallback, useEffect, useMemo } from 'react';
import { toast } from 'react-hot-toast';
import useSWR from 'swr'; import useSWR from 'swr';
import { z } from 'zod'; import { z } from 'zod';
import { themeStorageKey } from '@/consts'; import { themeStorageKey } from '@/consts';
import useApi, { RequestError } from './use-api'; import useApi, { RequestError } from './use-api';
import useLogtoUserId from './use-logto-user-id';
const userPreferencesGuard = z.object({ const userPreferencesGuard = z.object({
language: z.enum(languageKeys).optional(), language: z.enum(languageKeys).optional(),
@ -28,9 +31,10 @@ const getEnumFromArray = <T extends string>(
const useUserPreferences = () => { const useUserPreferences = () => {
const { isAuthenticated, error: authError } = useLogto(); const { isAuthenticated, error: authError } = useLogto();
const shouldFetch = isAuthenticated && !authError; const userId = useLogtoUserId();
const shouldFetch = isAuthenticated && !authError && userId;
const { data, mutate, error } = useSWR<unknown, RequestError>( const { data, mutate, error } = useSWR<unknown, RequestError>(
shouldFetch && '/api/users/me/custom-data' shouldFetch && `/api/users/${userId}/custom-data`
); );
const api = useApi(); const api = useApi();
@ -49,8 +53,14 @@ const useUserPreferences = () => {
const userPreferences = useMemo(() => parseData(), [parseData]); const userPreferences = useMemo(() => parseData(), [parseData]);
const update = async (data: Partial<UserPreferences>) => { const update = async (data: Partial<UserPreferences>) => {
if (!userId) {
toast.error(t('errors.unexpected_error'));
return;
}
const updated = await api const updated = await api
.patch('/api/users/me/custom-data', { .patch(`/api/users/${userId}/custom-data`, {
json: { json: {
customData: { customData: {
[key]: { [key]: {

View file

@ -9,6 +9,7 @@ import FormField from '@/components/FormField';
import ModalLayout from '@/components/ModalLayout'; import ModalLayout from '@/components/ModalLayout';
import TextInput from '@/components/TextInput'; import TextInput from '@/components/TextInput';
import useApi from '@/hooks/use-api'; import useApi from '@/hooks/use-api';
import useLogtoUserId from '@/hooks/use-logto-user-id';
import * as modalStyles from '@/scss/modal.module.scss'; import * as modalStyles from '@/scss/modal.module.scss';
import * as styles from './ChangePassword.module.scss'; import * as styles from './ChangePassword.module.scss';
@ -23,12 +24,19 @@ const ChangePassword = () => {
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const { watch, register, reset } = useForm<FormFields>(); const { watch, register, reset } = useForm<FormFields>();
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const userId = useLogtoUserId();
const api = useApi(); const api = useApi();
const password = watch('password'); const password = watch('password');
const confirmPassword = watch('confirmPassword'); const confirmPassword = watch('confirmPassword');
const isDisabled = !password || password !== confirmPassword; const isDisabled = !password || password !== confirmPassword;
const onSubmit = async () => { const onSubmit = async () => {
if (!userId) {
toast.error(t('errors.unexpected_error'));
return;
}
setIsLoading(true); setIsLoading(true);
await api.patch(`/api/users/me/password`, { json: { password } }).json(); await api.patch(`/api/users/me/password`, { json: { password } }).json();
setIsLoading(false); setIsLoading(false);