【问题标题】:Error: Unknown authentication strategy "local"错误:未知的身份验证策略“本地”
【发布时间】:2022-01-11 06:36:52
【问题描述】:

我正在使用护照对用户进行身份验证,我编写了以下代码: app.ts:

app.get('/login', function(req, res){
    res.render('login');
})


app.use(passport.initialize());
app.post('/login',
    passport.authenticate('local',{
        successRedirect:'/',
        failureRedirect:'/hhhhh',
        failureFlash: true
    })
)
const port = 4000;
app.listen(port, ()=>{
    console.log(`go to http://localhost:${port}`);
    
})

和 localStrategy.ts:

import { Strategy } from "passport-local";
passport.use(new Strategy(
    function(username: string, password: string, done){
        let user = users.find(u=>username === u.name);
        console.log(user);
        
        if (!user){
            return done(null, false, {message:'incorrect username'});
        }
        if(! (password == user.pw)){
            return done(null, false, {message:'incorrect password'})
        }
        return done(null, user)
    }
))

我得到错误: 错误:未知身份验证策略“本地”。

我在答案中发现我应该在初始化护照后添加类似的代码行:

require('./path/to/passport/config/file')(passport);

但是我不知道怎么用打字稿写出来,请帮忙

【问题讨论】:

    标签: node.js typescript express passport.js


    【解决方案1】:

    import { Strategy } from "passport-local"; 更改为 import { LocalStrategy } from "passport-local,如本地护照文档中所述:https://github.com/jaredhanson/passport-local/

    【讨论】:

      猜你喜欢
      • 2014-02-22
      • 2021-12-15
      • 1970-01-01
      • 2018-06-08
      • 2022-07-19
      • 2017-05-25
      • 2016-02-28
      • 2022-01-10
      • 1970-01-01
      相关资源
      最近更新 更多