简体   繁体   English

Passport.js本地策略没有猫鼬

[英]Passport.js local strategy WITHOUT mongoose

I'm trying to find resources about the most simple login with passport for a node app. 我正在尝试使用护照为节点应用程序找到最简单的登录资源。 I mean, using: 我的意思是,使用:

middleware: 中间件:

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: false}));
    app.use(cookieParser());

    app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
    app.use(passport.initialize());
    app.use(passport.session());

    app.use(express.static('public'));

passport: 护照:

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

passport.deserializeUser(function(id, done) {
    done({ id: id, nickname: "test"})
});


    passport.use(new LocalStrategy(
      function(username, password, done) {
        console.log("test");
          if (username === 'username') {
              return done(null, { name: "test", id: '1234'});
          } else {
              return done(null, false, { message: 'Incorrect cred.' });
          }
      })
    )

and redirection: 和重定向:

    app.post('/login',
      passport.authenticate('local', { 
          successRedirect: '/index.html',
          failureRedirect: '/login'
     })
    );

my app structure: 我的app结构:

/app.js
/public/index.html
/public/login.html

And that's it. 就是这样。 the Web is full of the same examples of Mongoose with plugins, everyone simply copy-pasting from each other. Web上充满了与插件相同的Mongoose示例,每个人都只是简单地相互粘贴。

The point is that I would like, later on, to insert my own code ti the LocalStrategy code (would probably used LDAP). 关键是我希望稍后在LocalStrategy代码中插入我自己的代码(可能会使用LDAP)。

Currently, instead of redirecting to /index.html with the page created in the public folder, I just receive [object Object] on index.html. 目前,我没有使用在公共文件夹中创建的页面重定向到/index.html,而是在index.html上接收[object Object]

Your passport.deserializeUser() is incorrect: 您的passport.deserializeUser()不正确:

passport.deserializeUser(function(id, done) {
  done({ id: id, nickname: "test"})
});

It's passing the "user" object as first argument to done , which is reserved for errors. 它将“user”对象作为第一个参数传递给done ,它是为错误保留的。 This causes Express to render an error page where it tries to stringify the error, resulting in [object Object] . 这会导致Express呈现错误页面,它会尝试将错误字符串化,从而产生[object Object]

Try this: 尝试这个:

passport.deserializeUser(function(id, done) {
  done(null, { id: id, nickname: "test"})
});

Found the issue! 发现了这个问题! the redirection didn't work, so I looked back up and noticed that the express.static mw was set AFTER the passport initialize(), and not BEFORE. 重定向不起作用,所以我回头看了一下,注意到express.static mw是在护照initialize()之后设置的,而不是BEFORE。 I just moved it up to the top and.. rainbows 我把它移到了顶部和..彩虹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM