Repository: onflow/fcl-js
Pull request #2119 (2025-02-05 20:31:26)

Setup disconnect
https://github.com/onflow/fcl-js/issues/2107

changeset-bot[bot]: ###  ⚠️  No Changeset found

Latest commit: ada9a0f50d84cdb31cc7c30b0e43daf3b0929186

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.**

<details><summary>This PR includes no changesets</summary>

  When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

</details>

[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add a changeset to this PR](https://github.com/onflow/fcl-js/new/cf/disconnect-2?filename=.changeset/mean-coins-peel.md&value=---%0A%22%40onflow%2Ffcl-ethereum-provider%22%3A%20patch%0A---%0A%0ASetup%20disconnect%0A)



Patch:
diff --git a/packages/fcl-ethereum-provider/src/accounts/account-manager.ts b/packages/fcl-ethereum-provider/src/accounts/account-manager.ts
index 7d470e10..4ff515ef 100644
--- a/packages/fcl-ethereum-provider/src/accounts/account-manager.ts
+++ b/packages/fcl-ethereum-provider/src/accounts/account-manager.ts
@@ -82,6 +82,14 @@ export class AccountManager {
       .subscribe(this.$addressStore)
   }
 
+  public async authenticate(): Promise<void> {
+    return await this.user.authenticate()
+  }
+
+  public async unauthenticate(): Promise<void> {
+    await this.user.unauthenticate()
+  }
+
   private async fetchCOAFromFlowAddress(flowAddr: string): Promise<string> {
     const cadenceScript = `
       import EVM
diff --git a/packages/fcl-ethereum-provider/src/create-provider.ts b/packages/fcl-ethereum-provider/src/create-provider.ts
index 73a7f9e3..da1f783d 100644
--- a/packages/fcl-ethereum-provider/src/create-provider.ts
+++ b/packages/fcl-ethereum-provider/src/create-provider.ts
@@ -50,7 +50,11 @@ export function createProvider(config: {
   })
   const rpcProcessor = new RpcProcessor(gateway, accountManager, networkManager)
   const eventProcessor = new EventDispatcher(accountManager, networkManager)
-  const provider = new FclEthereumProvider(rpcProcessor, eventProcessor)
+  const provider = new FclEthereumProvider(
+    accountManager,
+    rpcProcessor,
+    eventProcessor
+  )
 
   return provider
 }
diff --git a/packages/fcl-ethereum-provider/src/provider.ts b/packages/fcl-ethereum-provider/src/provider.ts
index 01ab36dc..ee6d9ed3 100644
--- a/packages/fcl-ethereum-provider/src/provider.ts
+++ b/packages/fcl-ethereum-provider/src/provider.ts
@@ -7,9 +7,11 @@ import {
 } from "./types/provider"
 import {RpcProcessor} from "./rpc/rpc-processor"
 import {EventDispatcher} from "./events/event-dispatcher"
+import {AccountManager} from "./accounts/account-manager"
 
 export class FclEthereumProvider implements Eip1193Provider {
   constructor(
+    private acountManager: AccountManager,
     private rpcProcessor: RpcProcessor,
     private eventDispatcher: EventDispatcher
   ) {}
@@ -30,6 +32,10 @@ export class FclEthereumProvider implements Eip1193Provider {
     }
   }
 
+  disconnect(): void {
+    this.acountManager.unauthenticate()
+  }
+
   // Listen to events
   on<E extends keyof ProviderEvents>(
     event: E,
diff --git a/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.ts b/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.ts
index 9ed4b2a9..54508fda 100644
--- a/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.ts
+++ b/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.ts
@@ -1,4 +1,3 @@
-import * as fcl from "@onflow/fcl"
 import {AccountManager} from "../../accounts/account-manager"
 
 export async function ethAccounts(
@@ -8,7 +7,7 @@ export async function ethAccounts(
 }
 
 export async function ethRequestAccounts(accountManager: AccountManager) {
-  await fcl.currentUser().authenticate()
+  await accountManager.authenticate()
 
   return await accountManager.getAccounts()
 }


Test patch:
diff --git a/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.test.ts b/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.test.ts
index 181916ee..c849a9cd 100644
--- a/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.test.ts
+++ b/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.test.ts
@@ -1,17 +1,5 @@
-// eth-accounts.spec.ts
 import {ethAccounts, ethRequestAccounts} from "./eth-accounts"
 import {AccountManager} from "../../accounts/account-manager"
-import * as fcl from "@onflow/fcl"
-import {CurrentUser} from "@onflow/typedefs"
-import {mockUser} from "../../__mocks__/fcl"
-
-// Mock FCL at the top-level
-jest.mock("@onflow/fcl", () => ({
-  currentUser: jest.fn().mockReturnValue({
-    authenticate: jest.fn(),
-    snapshot: jest.fn(),
-  }),
-}))
 
 describe("ethAccounts handler", () => {
   let accountManagerMock: jest.Mocked<AccountManager>
@@ -45,14 +33,10 @@ describe("ethAccounts handler", () => {
 
 describe("ethRequestAccounts handler", () => {
   let accountManagerMock: jest.Mocked<AccountManager>
-  let userMock: jest.Mocked<typeof fcl.currentUser>
 
   beforeEach(() => {
-    userMock = fcl.currentUser() as jest.Mocked<typeof fcl.currentUser>
-
-    userMock.snapshot.mockResolvedValue({addr: null} as unknown as CurrentUser)
-
     accountManagerMock = {
+      authenticate: jest.fn(),
       getAccounts: jest.fn(),
       updateCOAAddress: jest.fn(),
       subscribe: jest.fn(),
@@ -64,7 +48,7 @@ describe("ethRequestAccounts handler", () => {
 
     const accounts = await ethRequestAccounts(accountManagerMock)
 
-    expect(userMock.authenticate).toHaveBeenCalled()
+    expect(accountManagerMock.authenticate).toHaveBeenCalled()
     expect(accountManagerMock.getAccounts).toHaveBeenCalled()
     expect(accounts).toEqual(["0x1234..."])
   })
@@ -74,7 +58,7 @@ describe("ethRequestAccounts handler", () => {
 
     const accounts = await ethRequestAccounts(accountManagerMock)
 
-    expect(userMock.authenticate).toHaveBeenCalled()
+    expect(accountManagerMock.authenticate).toHaveBeenCalled()
     expect(accountManagerMock.getAccounts).toHaveBeenCalled()
     expect(accounts).toEqual([])
   })