Skip to content

Commit

Permalink
Clear in-app wallet state on disconnect (#3071)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregfromstl committed May 19, 2024
1 parent b46173f commit da63eab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-pens-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fully clear in-app wallet user data when disconnecting
9 changes: 8 additions & 1 deletion packages/thirdweb/src/wallets/create-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { ThirdwebClient } from "../client/client.js";
import { getContract } from "../contract/contract.js";
import { isContractDeployed } from "../utils/bytecode/is-contract-deployed.js";
import { COINBASE } from "./constants.js";
import { logoutAuthenticatedUser } from "./in-app/core/authentication/index.js";
import { DEFAULT_ACCOUNT_FACTORY } from "./smart/lib/constants.js";
import type { WCConnectOptions } from "./wallet-connect/types.js";
import { createWalletEmitter } from "./wallet-emitter.js";
Expand Down Expand Up @@ -495,7 +496,13 @@ export function inAppWallet(
return account;
},
disconnect: async () => {
// simply un-set the states
// If no client is assigned, we should be fine just unsetting the states
if (client) {
const result = await logoutAuthenticatedUser({ client });
if (!result.success) {
throw new Error("Failed to logout");
}
}
account = undefined;
chain = undefined;
emitter.emit("disconnect", undefined);
Expand Down
16 changes: 13 additions & 3 deletions packages/thirdweb/src/wallets/in-app/core/authentication/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
import type { InAppWalletSdk } from "../../implementations/lib/in-app-wallet.js";
import type { AuthArgsType, PreAuthArgsType } from "./type.js";

export type GetAuthenticatedUserParams = {
client: ThirdwebClient;
};

const ewsSDKCache = new WeakMap<ThirdwebClient, InAppWalletSdk>();

/**
Expand All @@ -31,9 +35,15 @@ async function getInAppWalletSDK(client: ThirdwebClient) {
return ewSDK;
}

export type GetAuthenticatedUserParams = {
client: ThirdwebClient;
};
/**
* @internal
*/
export async function logoutAuthenticatedUser(
options: GetAuthenticatedUserParams,
) {
const ewSDK = await getInAppWalletSDK(options.client);
return ewSDK.auth.logout();
}

/**
* Retrieves the authenticated user for the active in-app wallet.
Expand Down

0 comments on commit da63eab

Please sign in to comment.