Skip to content

Commit

Permalink
fix: adding try-catch to detect unauth
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhovnitsky committed Oct 8, 2024
1 parent ec0fd99 commit 3c6dd06
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 42 deletions.
24 changes: 17 additions & 7 deletions app/engine/hooks/holders/useCardTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CardNotification, fetchCardsTransactions } from "../../api/holders/fetc
import { Queries } from "../../queries";
import { deleteHoldersToken, useHoldersAccountStatus } from "./useHoldersAccountStatus";
import { HoldersUserState } from "../../api/holders/fetchUserState";
import axios from "axios";

export function useCardTransactions(address: string, id: string) {
let status = useHoldersAccountStatus(address).data;
Expand All @@ -24,15 +25,24 @@ export function useCardTransactions(address: string, id: string) {
},
queryFn: async (ctx) => {
if (!!status && status.state !== HoldersUserState.NeedEnrollment) {
const cardRes = await fetchCardsTransactions(status.token, id, 40, ctx.pageParam?.lastCursor, 'desc');
try {
const cardRes = await fetchCardsTransactions(status.token, id, 40, ctx.pageParam?.lastCursor, 'desc');

if (!cardRes) {
deleteHoldersToken(address);
throw new Error('Unauthorized');
}
if (!cardRes) {
deleteHoldersToken(address);
throw new Error('Unauthorized');
}

if (!!cardRes) {
return cardRes;
if (!!cardRes) {
return cardRes;
}
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 401) {
deleteHoldersToken(address);
throw new Error('Unauthorized');
} else {
throw error;
}
}
}
return null;
Expand Down
24 changes: 17 additions & 7 deletions app/engine/hooks/holders/useHoldersAccountStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { storage } from "../../../storage/storage";
import { HoldersUserState, userStateCodec, fetchUserState } from "../../api/holders/fetchUserState";
import { z } from 'zod';
import { removeProvisioningCredentials } from "../../holders/updateProvisioningCredentials";
import axios from "axios";

const holdersAccountStatus = z.union([
z.object({ state: z.literal(HoldersUserState.NeedEnrollment) }),
Expand All @@ -30,7 +31,7 @@ function migrateHoldersToken(addressString: string) {

export function deleteHoldersToken(address: string) {
// clean up provisioning credentials cache for this address
removeProvisioningCredentials(address);
removeProvisioningCredentials(address);
storage.delete(`holders-jwt-${address}`);
}

Expand Down Expand Up @@ -66,14 +67,23 @@ export function useHoldersAccountStatus(address: string | Address) {
return { state: HoldersUserState.NeedEnrollment } as HoldersAccountStatus; // This looks amazingly stupid
}

const fetched = await fetchUserState(token, isTestnet);
try {
const fetched = await fetchUserState(token, isTestnet);

if (!fetched) { // unauthorized
deleteHoldersToken(addr);
return { state: HoldersUserState.NeedEnrollment } as HoldersAccountStatus;
}
if (!fetched) { // unauthorized
deleteHoldersToken(addr);
return { state: HoldersUserState.NeedEnrollment } as HoldersAccountStatus;
}

return { ...fetched, token } as HoldersAccountStatus;
return { ...fetched, token } as HoldersAccountStatus;
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 401) {
deleteHoldersToken(addressString);
throw new Error('Unauthorized');
} else {
throw error;
}
}
},
refetchOnWindowFocus: true,
refetchOnMount: true,
Expand Down
66 changes: 38 additions & 28 deletions app/engine/hooks/holders/useHoldersAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { GeneralHoldersAccount, PrePaidHoldersCard, fetchAccountsList, fetchAcco
import { deleteHoldersToken, useHoldersAccountStatus } from "./useHoldersAccountStatus";
import { HoldersUserState } from "../../api/holders/fetchUserState";
import { updateProvisioningCredentials } from "../../holders/updateProvisioningCredentials";
import axios from "axios";

export type HoldersAccounts = {
accounts: GeneralHoldersAccount[],
type: 'public' | 'private',
accounts: GeneralHoldersAccount[],
type: 'public' | 'private',
prepaidCards?: PrePaidHoldersCard[]
}

Expand Down Expand Up @@ -38,38 +39,47 @@ export function useHoldersAccounts(address: string | Address) {
refetchInterval: 35000,
staleTime: 35000,
queryFn: async () => {
let accounts;
let prepaidCards: PrePaidHoldersCard[] | undefined;
let type = 'public';

if (token) {
const res = await fetchAccountsList(token, isTestnet);
try {
let accounts;
let prepaidCards: PrePaidHoldersCard[] | undefined;
let type = 'public';

if (!res) {
deleteHoldersToken(addressString);
throw new Error('Unauthorized');
}
if (token) {
const res = await fetchAccountsList(token, isTestnet);

type = 'private';
accounts = res?.accounts;
prepaidCards = res?.prepaidCards;
if (!res) {
deleteHoldersToken(addressString);
throw new Error('Unauthorized');
}

// fetch apple pay credentials and update provisioning credentials cache
await updateProvisioningCredentials(addressString, isTestnet);
} else {
accounts = await fetchAccountsPublic(addressString, isTestnet);
type = 'public';
}
type = 'private';
accounts = res?.accounts;
prepaidCards = res?.prepaidCards;

// fetch apple pay credentials and update provisioning credentials cache
await updateProvisioningCredentials(addressString, isTestnet);
} else {
accounts = await fetchAccountsPublic(addressString, isTestnet);
type = 'public';
}

const filtered = accounts?.filter((a) => a.network === (isTestnet ? 'ton-testnet' : 'ton-mainnet'));
const filtered = accounts?.filter((a) => a.network === (isTestnet ? 'ton-testnet' : 'ton-mainnet'));

const sorted = filtered?.sort((a, b) => {
if (a.cards.length > b.cards.length) return -1;
if (a.cards.length < b.cards.length) return 1;
return 0;
});
const sorted = filtered?.sort((a, b) => {
if (a.cards.length > b.cards.length) return -1;
if (a.cards.length < b.cards.length) return 1;
return 0;
});

return { accounts: sorted, type, prepaidCards } as HoldersAccounts;
return { accounts: sorted, type, prepaidCards } as HoldersAccounts;
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 401) {
deleteHoldersToken(addressString);
throw new Error('Unauthorized');
} else {
throw error;
}
}
}
});

Expand Down

0 comments on commit 3c6dd06

Please sign in to comment.