mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 13:29:41 +02:00
32 lines
851 B
TypeScript
32 lines
851 B
TypeScript
import { useQuery } from 'react-query';
|
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
|
import { withError } from '@/react-tools/react-query';
|
|
|
|
import { buildUrl } from '../user.service';
|
|
import { User } from '../types';
|
|
|
|
import { queryKeys } from './queryKeys';
|
|
|
|
interface CurrentUserResponse extends User {
|
|
forceChangePassword: boolean;
|
|
}
|
|
|
|
export function useLoadCurrentUser({ staleTime }: { staleTime?: number } = {}) {
|
|
return useQuery(queryKeys.me(), () => getCurrentUser(), {
|
|
...withError('Unable to retrieve user details'),
|
|
staleTime,
|
|
});
|
|
}
|
|
|
|
export async function getCurrentUser() {
|
|
try {
|
|
const { data: user } = await axios.get<CurrentUserResponse>(
|
|
buildUrl(undefined, 'me')
|
|
);
|
|
|
|
return user;
|
|
} catch (e) {
|
|
throw parseAxiosError(e as Error, 'Unable to retrieve user details');
|
|
}
|
|
}
|