title: How to use passport with next.js API?
body: <p>I'm trying to use <a href="https://github.com/jmperez/passport-spotify" rel="nofollow noreferrer">passport-spotify</a> with next.js's pages/api (the one where you use <code>export default (req, res)...</code>), but I can't get it to redirect to Spotify's authorization page. This is my code for <code>pages/api/spotify.js</code>:</p>
  <pre><code>var passport = require('passport');
  const SpotifyStrategy = require('passport-spotify').Strategy;
  
  passport.use(
    new SpotifyStrategy(
      {
        clientID: clientid,
        clientSecret: clientsecret,
        callbackURL: 'http://localhost:3000/auth/spotify/callback'
      },
      function(accessToken, refreshToken, expires_in, profile, done) {
        User.findOrCreate({ spotifyId: profile.id }, function(err, user) {
          return done(err, user);
        });
      }
    )
  );
  
  export default (req, res, next) =&gt; {
      passport.authenticate('spotify', {
          scope: ['user-read-email', 'user-read-private'],
          showDialog: true
        }),
      res.end()
  }
  </code></pre>
  <p>I've tried using express to test it, and it works there. Here's my code:</p>
  <pre><code>const express = require('express')
  const app = express()
  const port = 8000
  var passport = require('passport');
  const SpotifyStrategy = require('passport-spotify').Strategy;
  
  passport.use(
    new SpotifyStrategy(
      {
        clientID: clientid,
        clientSecret: clientsecret,
        callbackURL: 'http://localhost:3000/api/callback'
      },
      function(accessToken, refreshToken, expires_in, profile, done) {
        User.findOrCreate({ spotifyId: profile.id }, function(err, user) {
          return done(err, user);
        });
      }
    )
  );
  app.get(
      '/auth/spotify',
      passport.authenticate('spotify', {
        scope: ['user-top-read'],
        showDialog: true
      }),
      function(req, res) {
        // The request will be redirected to spotify for authentication, so this
        // function will not be called.
      }
    );
  
  app.listen(port, () =&gt; {
    console.log(`Example app listening at http://localhost:${port}`)
  })
  </code></pre>
  <p>Is there a way to have Next.js work with Passport?</p>