passport-spotify icon indicating copy to clipboard operation
passport-spotify copied to clipboard

`VerifyFunction` parameters are passed in the wrong order

Open NilsSutter opened this issue 3 years ago • 4 comments

Hey everyone, I was trying to setup an express-api where users would always authorize and authenticate via spotify, for this I wanted to use passport-spotify.

I'm also using typescript and a mongoDB. From the documentation, this was the setup to use the SpotifyStrategy:

passport.use(
  new SpotifyStrategy(
    {
      clientID: client_id,
      clientSecret: client_secret,
      callbackURL: 'http://localhost:8888/auth/spotify/callback',
    },
    function (accessToken, refreshToken, expires_in, profile, done) {
      User.findOrCreate({spotifyId: profile.id}, function (err, user) {
        return done(err, user);
      });
    }
  )
);

Problem However, the arguments seem to be passed in in the wrong order, at least the typescript compiler will complain about it, i.e.

Screenshot 2021-04-11 at 13 36 30

Therefore, it seems to be that the right order of function parameters for the VerifyFunction should be as followed: async(_req, accessToken, refreshToken, profile, done, expires_in) => {...}

However, when this is being done, I will get the following mongoose validation errors :

{ Error: User validation failed: spotifyId: Path `spotifyId` is required., userName: Path `userName` is required.
    at ValidationError.inspect (/Users/nilssutter/Desktop/spotify_swipes/node_modules/mongoose/lib/error/validation.js:47:26)
    at formatValue (internal/util/inspect.js:522:31)
    at inspect (internal/util/inspect.js:197:10)
    at formatWithOptions (internal/util/inspect.js:1390:12)
    at Console.(anonymous function) (internal/console/constructor.js:277:10)
    at Console.log (internal/console/constructor.js:287:61)
    at Function.UserRepository.findAndUpdateOrCreate

The reason for this is that profile is still undefined.

Workaround It might be that I'm messing something up, but my current workaround is smart-casting the parameter to the type they actually should have, i.e.:

Screenshot 2021-04-11 at 13 45 06

So yeah, I'm a bit confused :D

This currently works well for me but I suppose the arguments for the VerifyFunction should come in in another order in the first place?

NilsSutter avatar Apr 11 '21 11:04 NilsSutter

I think the issue is that @types/passport-spotify specifies the wrong order for the arguments. I'm going to create an MR to add types to the library. That is the recommended approach for libraries these days.

alexstaroselsky avatar May 04 '21 22:05 alexstaroselsky

Hey @alexstaroselsky . Thanks for pointing this out :)

NilsSutter avatar May 05 '21 11:05 NilsSutter

I created a PR for @types/passport-spotify. Once that is approved/merged at least that issue for the verity function signature should be resolved. That being said, you still would likely need to use type casting or similar for passport.authenticate as showDialog (if you are using that) is not supported by AuthenticateOptions

alexstaroselsky avatar May 05 '21 14:05 alexstaroselsky

I created a PR for @types/passport-spotify. Once that is approved/merged at least that issue for the verity function signature should be resolved. That being said, you still would likely need to use type casting or similar for passport.authenticate as showDialog (if you are using that) is not supported by AuthenticateOptions

Hi! Your PR didnt pass.

LarissaGuder avatar Oct 21 '21 18:10 LarissaGuder