mxstbr / passport-magic-login

Passwordless authentication with magic links for Passport.js.
MIT License
664 stars 45 forks source link

error TS2351: This expression is not constructable. #37

Open guidev opened 5 months ago

guidev commented 5 months ago

I get the following error when trying to use it with typescript:

error TS2351: This expression is not constructable.
  Type 'typeof import("/Users/guiguiguigui93/Progetti/aibattle/node_modules/passport-magic-login/dist/index")' has no construct signatures.

my code

const magicLogin = new MagicLoginStrategy({
    secret: process.env.MAGIC_LINK_SECRET,
    callbackUrl: "/auth/magiclogin/callback",
    sendMagicLink: async (destination, href) => {
        console.log("sendMagicLink", destination, href);
    },
    verify: (payload, callback) => {
        console.log("verify", payload);
    },
    jwtOptions: {
        expiresIn: "2 days",
    }
})
aarhusgregersen commented 3 months ago

Heya!

I'm getting the exact same thing during a conversion from CJS to ESM. I suspect it has to do with the way imports are handled in ESM, but that's about all I know about it.

Are you using CJS or ESM, @guidev ? Additionally, did you manage to find a solution or workaround?

If not I would think that the "trick" that works for some of the other strategy packages would work: const MagicLoginStrategy = require("passport-magic-login").Strategy

Do note I haven't tested or verified this, it's just something I suspect might work

aarhusgregersen commented 3 months ago

After a bit of googling it turns out there is an answer for this: Simply do:

import MagicLoginStrategy from "passport-magic-login";

const magicLogin = new MagicLoginStrategy.default();

https://stackoverflow.com/a/72676003/5065467