【问题标题】:How to use passport with next.js API?如何在 next.js API 中使用护照?
【发布时间】:2020-08-24 03:54:59
【问题描述】:

我正在尝试将 passport-spotify 与 next.js 的 pages/api(您使用 export default (req, res)... 的那个)一起使用,但我无法将其重定向到 Spotify 的授权页面。这是我的pages/api/spotify.js 代码:

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) => {
    passport.authenticate('spotify', {
        scope: ['user-read-email', 'user-read-private'],
        showDialog: true
      }),
    res.end()
}

我试过用 express 来测试它,它在那里工作。这是我的代码:

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, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

有没有办法让 Next.js 与 Passport 一起工作?

【问题讨论】:

    标签: node.js passport.js next.js


    【解决方案1】:

    我明白了!我用next-connect。这是一个带有 passport-spotify 和 next.js 的 Spotify 身份验证请求示例:

    // pages/api/spotify.js
    import nc from 'next-connect';
    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);
          })
        }
      ))
    
    const handler = nc()
      .get(passport.authenticate('spotify', {
        scope: ['user-top-read']
      }), (req, res) => {
      })
    
    export default handler;
    

    【讨论】:

      猜你喜欢
      • 2023-02-21
      • 2021-02-17
      • 2016-05-09
      • 1970-01-01
      • 2020-09-09
      • 2021-05-14
      • 2012-10-17
      • 1970-01-01
      • 2018-08-07
      相关资源
      最近更新 更多