magiclabs / magic-js

Magic browser/React Native JavaScript SDK is your entry-point to integrating passwordless authentication inside your application.
https://magic.link/docs/api-reference/client-side-sdks/web
Apache License 2.0
462 stars 86 forks source link

Next.js + Magic SDK V6 + OAuth Extension Fails with `TypeError: class constructors must be invoked with 'new'` #216

Closed frankfka closed 2 years ago

frankfka commented 3 years ago

✅ Prerequisites

🐛 Description

When initializing the client with:

export const createClientMagicInstance = () => {
  if (typeof window === 'undefined') {
    throw Error('Creating magic instance without browser window');
  }
  return new Magic(magicPublicKey, {
    extensions: [new OAuthExtension()],
  });

within Next.js, the following error is thrown class constructors must be invoked with 'new'.

This only happens when calling new OAuthExtension(). The issue is gone when downgrading to "magic-sdk": "^5.1.0"

🌎 Environment

Software Version(s)
magic-sdk 6.0.5
@magic-ext/oauth 0.9.0
next 11.1.2
Browser Firefox
yarn 16.7
Operating System MacOS
smithki commented 3 years ago

Thanks for the report. This appears to be related to the v6.x change to the Magic JS build system. We introduced this change to support <script type="module" /> targets. The unintended side-effect is that our OAuthExtension is still compiled to ES5 by default. So, this creates a problem where the ES5-formatted OAuthExtension class extends from the ES6-formatted Extension class inside of magic-sdk.

We have plans to migrate our Magic JS extension code to this monorepo, where it will share a build system with the rest of Magic SDK (thus resolving the issue). We'll leave this issue open to track the status of that change.

For now, pointing at magic-sdk@5.x should resolve the problem. There are no features at this time in magic-sdk@6.x which are missing from 5.x releases.

smithki commented 3 years ago

This issue has been resolved in magic-sdk@6.0.7 and @magic-sdk/oauth@0.9.2, as well as for these other extensions:

Our Web3-related extensions will follow suit.

ideopunk commented 3 years ago

Looking forward to the web3 fixes, it's an issue with @magic-ext/solana@1.0.0.

smithki commented 3 years ago

@ideopunk 6.x was all about better ES module compatibility, no API changes. You can use 5.x in the meanwhile!

smithki commented 2 years ago

Closing this, as our remaining extensions have been ported to this monorepo!