magic-js icon indicating copy to clipboard operation
magic-js copied to clipboard

Farcaster example flaky

Open kurtisassad opened this issue 11 months ago • 0 comments

✅ Prerequisites

  • [x] Did you perform a cursory search of open issues? Is this bug already reported elsewhere?
  • [x] Are you running the latest SDK version?
  • [x] Are you reporting to the correct repository (magic-sdk)?

🐛 Description

In the farcaster docs (https://magic.link/docs/authentication/login/farcaster#example) we have:

import { Magic } from 'magic-sdk';
import { FarcasterExtension } from '@magic-ext/farcaster';

const magic = new Magic('YOUR_API_KEY', {
  extensions: [new FarcasterExtension()],
});

try {
  await magic.farcaster.login();
} catch {
  // Handle errors or reject if required!
}

But note that this sometimes fails if the request doesn't go through before login is called due to the async nature of the constructor:

(@magic-ext/farcaster/src/index.ts)

export class FarcasterExtension
...

constructor() {
    super();

    (async () => {
      this.channel = await fetch(`${FARCASTER_RELAY_URL}/v1/channel`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          domain: window.location.host,
          siweUri: window.location.origin,
        }),
      }).then<CreateChannelAPIResponse>((r) => r.json());
    })();
  }

public login = (params?: LoginParams) => {
    if (!this.channel) {
      throw new Error('Channel not created yet.');
    }
...

Note that in the case where the request in the constructor does not resolve before login is called, the error Channel not created yet. is thrown.

🧩 Steps to Reproduce

  1. Use the example in the docs
  2. Set network to 3G in browser dev tools

🤔 Expected behavior

Should work correctly and show the farcaster login UI

😮 Actual behavior

An error Channel not created yet. is thrown.

💻 Code Sample

[If possible, please provide a code repository, gist, code snippet or sample files to reproduce the issue.]

🌎 Environment

Software Version(s)
magic-sdk 28.17.0
Browser Chrome 130
pnpm 9.12.3
Operating System macos

kurtisassad avatar Nov 14 '24 09:11 kurtisassad