【问题标题】:Error: Unknown authentication strategy "spotify"错误:未知的身份验证策略“spotify”
【发布时间】:2022-01-10 16:00:31
【问题描述】:

当我尝试我的代码“错误:未知身份验证策略“spotify”时出现错误。我什至尝试用谷歌搜索这个问题,但找不到答案

更新:我更新了我的代码,以便为帮助我的人提供答案

代码

const passport = require('passport')
const SpotifyStrategy = require('passport-spotify').Strategy
const userWebData = require('../model/userWebSchema')

passport.serializeUser((user, done) => {
    const u = user
    done(null, u)
})
passport.deserializeUser(async(spotifyID, done)=> {
    try{
        const data = await userWebData.findOne({ spotifyID: spotifyID})
        const user = data
        return user ? done( null, user) : done(null, null)
    }catch(e){
        console.log(e)
        done(e, null)
    }
})
passport.use(
    new SpotifyStrategy({
        clientID: 'Clients_ID_That_I_Wont_Show',
        clientSecret: 'Clients_Secret_That_I_Wont_Show',
        callbackURL: 'http://localhost:3001/',
        scope: ["user-modify-playback-state", "user-read-private","user-library-read"]
    }, async(accessToken, refreshToken, expires_in, profile, done)=>{
        const userData = await userWebData.findOne({ spotifyID: profile.id})
        if(userData){
            console.log(`User ${profile.displayName} found`)
            return done(err, userData);
        }else{
            let profiles = await userWebData.create(
                {
                    spotifyID: profile.id,
                    spotifyName: profile.displayName,
                    spotifyFollowers: profile.followers,
                }
            )
            profile.save()
            console.log(`New User ${profile.displayName} Created`)
            return done(err, userData);
        }
    }
    )
)

【问题讨论】:

    标签: javascript node.js passport.js spotify


    【解决方案1】:

    您还必须在使用 passport-spotify 包进行身份验证之前导入它。首先,安装包 (npm i passport-spotify),然后在你的文件中 require 它:

    const SpotifyStrategy = require('passport-spotify').Strategy;
    

    然后,您必须先调用passport.use() 函数才能使用passport 进行身份验证:

    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);
          });
        }
      )
    );
    

    最后,您可以将其用作 Express 中间件。您修改后的代码如下所示:

    const express = require('express')
    const app = express()
    const passport = require('passport')
    const SpotifyStrategy = require('passport-spotify').Strategy;
    
    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);
          });
        }
      )
    );
    app.get('/', passport.authenticate('spotify'), (req, res)=>{
        res.redirect('http://localhost:3001/user')
    }) 
    app.get('/user', (req, res) => {
        if(req.user == undefined){res.status(401).redirect('http://localhost:3001/')}else{res.send(req.user)}
    })
    
    module.exports = app
    

    参考http://www.passportjs.org/packages/passport-spotify/

    【讨论】:

    • 是的,但这就是我所拥有的
    • 您是否安装并需要 passport-spotify 库?这是让它发挥作用的第一步。
    • 是的,我确实安装并需要
    • 你用正确的参数调用passport.use了吗?
    • 是的,我也这样做了
    猜你喜欢
    • 2015-04-03
    • 2022-01-11
    • 2014-02-22
    • 2017-04-01
    • 2021-12-15
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多