.
This commit is contained in:
1 parent
dfcfe9e4ad
commit
60928bc165
2 files changed
+43
-4
No files matched your search
@@ -1,6 +1,12 @@
|
||||
import { InteractionStatus } from '@azure/msal-browser';
|
||||
import { useMsal } from '@azure/msal-react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { DEFAULT_COMMUNITY_ID, useAuthUserId } from '../config';
|
||||
import { apiFetch } from '../lib/api';
|
||||
import {
|
||||
isMsalConfigured,
|
||||
MSAL_API_SCOPE,
|
||||
} from '../lib/auth/msal.config';
|
||||
import type {
|
||||
ApiAuction,
|
||||
ApiCommunity,
|
||||
@@ -93,10 +99,32 @@ export function useDdragonManifest(locale = 'ko_KR') {
|
||||
|
||||
export function useMe() {
|
||||
const authUserId = useAuthUserId();
|
||||
const { accounts, inProgress, instance } = useMsal();
|
||||
const account = isMsalConfigured
|
||||
? (instance.getActiveAccount() ?? accounts[0] ?? null)
|
||||
: null;
|
||||
const accountKey = account?.homeAccountId ?? null;
|
||||
const isMsalReady =
|
||||
!isMsalConfigured || inProgress === InteractionStatus.None;
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['me', authUserId],
|
||||
queryFn: () => apiFetch<MeResponse>('/me'),
|
||||
queryKey: ['me', authUserId, accountKey],
|
||||
queryFn: async () => {
|
||||
if (!account) {
|
||||
return apiFetch<MeResponse>('/me');
|
||||
}
|
||||
|
||||
const response = await instance.acquireTokenSilent({
|
||||
account,
|
||||
scopes: [MSAL_API_SCOPE],
|
||||
});
|
||||
return apiFetch<MeResponse>('/me', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${response.accessToken}`,
|
||||
},
|
||||
});
|
||||
},
|
||||
enabled: Boolean(authUserId || isMsalReady),
|
||||
retry: false,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user