【问题标题】:Passportjs Local strategy never get calledPassportjs 本地策略永远不会被调用
【发布时间】:2018-07-27 03:40:27
【问题描述】:

我正在学习passportjs,我的server.js中有以下代码,我面临的问题是下面代码中的passport.use('local',new LocalStrategy({})从未被调用过,除了localstrategy,其余的工作正常,一直在苦苦挣扎几个小时!

我正在学习passportjs,我的server.js中有以下代码,我面临的问题是下面代码中的passport.use('local',new LocalStrategy({})从未被调用过,除了localstrategy,其余工作正常,一直在苦苦挣扎几个小时!

const express=require("express");
const bodyParser=require('body-parser');
const ejs=require("ejs");
const cookieParser=require('cookie-parser');
const passport=require('passport');
const LocalStrategy=require('passport-local').Strategy;
const session=require("express-session");
const app=express();
app.set(express.static,'public');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({secret:'library'}));

app.use(passport.initialize());
app.use(passport.session());

app.post('/auth/signUp',(req,res,next)=>{
    console.log('body',req.body)
    passport.authenticate('local',(err,user,info)=>{
        console.log("whattt",user)
        req.login(user,(err)=>{
            res.redirect('/auth/profile');
        });
    })(req, res, next);
});

app.get('/auth/profile',(req,res)=>{

    res.json(req.user);
});


app.get("/",async (req,res)=>{

    res.render('index.ejs');
});

passport.serializeUser((user,done)=>{
    done(null,user.id);
});

passport.deserializeUser((id, done)=> {
    console.log("id",id);
    let data=[{id:1,name:"malouda"},{id:2,name:"Jason"}];
    let user = data.find((obj)=>{ return obj.id === id; });
    done(null,user);
});

passport.use('local',new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password'
},(username,password,done)=>{
    user={id:1,name:"malouda"};
    console.log("LocalStrategy")
    done(null,user);
})); 
app.listen(3000,()=>{
    console.log("Listening on port 6000");
});

在我的 index.js 中,我有以下形式

<form name="signUpForm" action="/auth/signUp" method="post" multipart='urlencoded'>
                <div class="form-group">
                    <label for="username">Username</label>
                    <input type="text" class="form-control" name="username" id="username" placeholder="Username">
                </div>
                <div class="form-group">
                    <label for="exampleInputPassword1">Password</label>
                    <input type="password" class="form-control" name="password " id="exampleInputPassword1" placeholder="Password">
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>

【问题讨论】:

    标签: node.js passport.js passport-local


    【解决方案1】:

    Passport LocalStrategy 用于登录验证。

    在注册中,您不需要护照。在注册中,您只需通过将用户存储在数据库中来创建用户。

    你需要添加

    Passport.authenticate(`local`, (err, user, info) => {}
    

    在您的 login API 中对用户进行身份验证。

    顾名思义,Passport.authenticate 对用户凭据进行身份验证。所以首先你需要创建用户。

    Read more here.

    希望我清除了你的想法。

    【讨论】:

      猜你喜欢
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多