Skip to content

Commit

Permalink
Fix success url issue (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenmarcus authored Feb 15, 2024
1 parent 8867950 commit d37dfbe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@testing-library/user-event": "^14.5.2"
},
"dependencies": {
"@mintbase-js/wallet": "0.5.2-beta.0",
"@mintbase-js/wallet": "0.5.5-fix-success-url-issue-765a756.0",
"@near-wallet-selector/core": "8.9.3",
"@near-wallet-selector/here-wallet": "8.9.3",
"@near-wallet-selector/meteor-wallet": "8.9.3",
Expand Down
30 changes: 25 additions & 5 deletions packages/react/src/MintbaseWalletContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
setupMintbaseWalletSelector,
} from './wallet/wallet';
import type { WalletSelectorComponents } from './wallet/wallet';

import type {
WalletSelector,
AccountState,
Expand All @@ -26,7 +27,8 @@ import type { WalletSelectorModal } from '@near-wallet-selector/modal-ui';

// This is heavily based on
// https://github.com/near/wallet-selector/blob/main/examples/react/contexts/WalletSelectorContext.tsx
// but uses wrappers from @mintbase-js/auth and @mintbase-js/sdk
// but uses wrappers from @mintbase-js/wallet and @mintbase-js/sdk

export type MintbaseWalletContext = {
selector: WalletSelector;
modal: WalletSelectorModal;
Expand All @@ -41,11 +43,28 @@ export type MintbaseWalletContext = {
signMessage: (params: VerifyOwnerParams) => Promise<VerifiedOwner>;
}

interface ContextProviderType {
children: React.ReactNode;
callbackUrl?: string;
network?: string; onlyMbWallet?: boolean;
contractAddress?: string;
additionalWallets?: Array<WalletModuleFactory>;
successUrl?: string;
failureUrl?: string;
}


export const MintbaseWalletContext = createContext<MintbaseWalletContext | null>(null);

// eslint-disable-next-line max-len
export const MintbaseWalletContextProvider: React.FC<{ children: React.ReactNode; callbackUrl?: string; network?: string; onlyMbWallet?: boolean; contractAddress?: string; additionalWallets?: Array<WalletModuleFactory> }> = ({
children, network, contractAddress, additionalWallets, onlyMbWallet, callbackUrl,
export const MintbaseWalletContextProvider: React.FC<ContextProviderType> = ({
children,
network,
contractAddress,
additionalWallets,
onlyMbWallet,
callbackUrl,
successUrl,
failureUrl,
}): JSX.Element => {
const [errorMessage, setErrorMessage] = useState<string>('');
const [components, setComponents] = useState<WalletSelectorComponents | null>(
Expand All @@ -70,6 +89,7 @@ export const MintbaseWalletContextProvider: React.FC<{ children: React.ReactNode
selectedNetwork,
selectedContract,
isOnlyMbWallet ? { additionalWallets } : undefined,
successUrl, failureUrl,
);
};

Expand All @@ -92,7 +112,7 @@ export const MintbaseWalletContextProvider: React.FC<{ children: React.ReactNode

// call setup on wallet selector


useEffect(() => {
setupWallet();

Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export const setupMintbaseWalletSelector = async (
network?,
contractAddress?,
options?: { additionalWallets?: Array<WalletModuleFactory> },
successUrl?: string,
failureUrl?: string,
): Promise<WalletSelectorComponents> => {


Expand All @@ -81,6 +83,8 @@ export const setupMintbaseWalletSelector = async (
setupMintbaseWallet({
walletUrl: walletUrls[network],
callbackUrl: callbackUrl,
successUrl: successUrl || window.location.href,
failureUrl: successUrl || window.location.href,
}),
...(options?.additionalWallets || []),
...SUPPORTED_NEAR_WALLETS,
Expand Down
6 changes: 3 additions & 3 deletions packages/wallet/src/mintbase-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,16 @@ export const MintbaseWallet: WalletBehaviourFactory<

const signIn = async (): Promise<MintbaseWalletAccount[]> => {
const existingAccounts = await getAccounts();
const origin = encodeURI(window?.location?.origin);
const href = encodeURI(window?.location?.href);

if (existingAccounts.length) {
return existingAccounts;
}

await state.wallet.requestSignIn({
methodNames: [],
successUrl: origin,
failureUrl: origin,
successUrl: successUrl || href,
failureUrl: failureUrl || href,
});

return getAccounts();
Expand Down

0 comments on commit d37dfbe

Please sign in to comment.