-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: authorize fetching * fix: token icon
- Loading branch information
Showing
8 changed files
with
56 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useCallback } from 'react' | ||
import { useAccount } from 'wagmi' | ||
|
||
import { fetchJSON } from '@/helpers/fetchJSON' | ||
import { useLogin } from '@/hooks/useLogin' | ||
import { useAccountStore } from '@/store/accountStore' | ||
import { Response } from '@/types/api' | ||
|
||
export function useAuthFetch<T extends Response<unknown>>() { | ||
const login = useLogin() | ||
const { token } = useAccountStore() | ||
const account = useAccount() | ||
return useCallback<typeof fetchJSON<T>>( | ||
async (input, init) => { | ||
const address = account.address | ||
if (!address) throw new Error('No wallet connected') | ||
let jwtToken = token | ||
if (!jwtToken) { | ||
jwtToken = await login.mutateAsync() | ||
} | ||
|
||
function send() { | ||
return fetchJSON<T>(input, { | ||
...init, | ||
headers: { | ||
Authorization: `Bearer ${jwtToken}`, | ||
}, | ||
}) | ||
} | ||
|
||
const res = await send() | ||
if (res.code === 401) { | ||
jwtToken = await login.mutateAsync() | ||
} | ||
return await send() | ||
}, | ||
[account.address, login, token], | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,19 @@ | ||
import { useCallback } from 'react' | ||
import urlcat from 'urlcat' | ||
import { useAccount } from 'wagmi' | ||
|
||
import { FIREFLY_API_ROOT } from '@/constants/api' | ||
import { fetchJSON } from '@/helpers/fetchJSON' | ||
import { useLogin } from '@/hooks/useLogin' | ||
import { useAccountStore } from '@/store/accountStore' | ||
import { useAuthFetch } from '@/hooks/useAuthFetch' | ||
import { CheckTokenResponse } from '@/types/api' | ||
|
||
export function useCheckStats() { | ||
const account = useAccount() | ||
|
||
const { token } = useAccountStore() | ||
const login = useLogin() | ||
|
||
const authFetch = useAuthFetch<CheckTokenResponse>() | ||
return useCallback(async () => { | ||
const address = account.address | ||
if (!address) return | ||
try { | ||
let jwtToken = token | ||
if (!jwtToken) { | ||
jwtToken = await login.mutateAsync() | ||
} | ||
|
||
const url = urlcat(FIREFLY_API_ROOT, 'v1/mask_stake/check_token') | ||
const res = await fetchJSON<CheckTokenResponse>(url, { | ||
headers: { | ||
Authorization: `Bearer ${jwtToken}`, | ||
}, | ||
}) | ||
const res = await authFetch(url) | ||
return res.data | ||
} catch { | ||
// | ||
} | ||
}, [account.address, token, login]) | ||
}, [authFetch]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
interface Response<T> { | ||
export interface Response<T> { | ||
code: number | ||
reason: string | ||
message: string | ||
|