javascript 使用通行证的 Node.js 用户身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10969299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:40:12  来源:igfitidea点击:

Node.js user authentication using passport

javascriptnode.jsexpresscoffeescriptpassport.js

提问by Marius Kjeldahl

(updated code with serialization functions - still redirects to /failedRedirect)

(使用序列化功能更新代码 - 仍然重定向到 /failedRedirect)

I'm trying to get simple username/password authentication going using the passport package, but failing. In the example below I've tried to verify that authentication works by basically always returning a valid authentication (regardless of what gets passed), but for some reason it fails and passport redirects to the failed login link.

我正在尝试使用护照包进行简单的用户名/密码身份验证,但失败了。在下面的示例中,我试图通过基本上总是返回有效的身份验证(无论通过什么)来验证身份验证是否有效,但由于某种原因它失败并且护照重定向到失败的登录链接。

If anybody could help me out in figuring out how to get this example to simply authenticate anything, I should be able to manage from there.

如果有人能帮我弄清楚如何让这个例子简单地验证任何东西,我应该能够从那里进行管理。

The code in coffeescript is:

coffeescript 中的代码是:

express = require "express"
passport = require "passport"
LocalStrategy = require("passport-local").Strategy

passport.use(new LocalStrategy( (username, password, done) ->
  console.log "LocalStrategy invoked"
  done(null, {id: 1, name: "Marius"})
))

passport.serializeUser (user, done) ->
  done null, user

passport.deserializeUser (obj, done) ->
  done null, obj

app = express.createServer()

app.configure ->
  app.use express.bodyParser()
  app.use express.static("./public")
  app.use express.cookieParser("SOMESECRET")
  app.use express.session
    secret: "SOMESECRET"
    cookie:
      maxAge: 60000
  app.use passport.initialize()
  app.use passport.session()
  app.set "view", "./srv/views"
  app.set "view engine", "jade"

app.get "/login", (req, res) ->
  res.send "login page"

app.post "/login", passport.authenticate("local",
  failureRedirect: "/failedRedirect"
  successRedirect: "/successRedirect"
  failureFlash: true)

app.listen 8082

Solved:Ok, I believe there were a few reasons why I could not get it working. The serialize stuff may be one (I haven't tested), but since Jared said they were needed, I'm leaving them in (he's the author of Passport). The other confusion may be related to express versions and my confusion with npm. I believe I tested both the latest v2 of express, but I've also tested v3, which I am running now. For version three, you probably should check out the connect-flashmodule on Github as well, as some to the "flash" stuff which is used in Jared's examples was moved out of express v3 (so the module puts it back in...). And finally, make sure you post using the proper named input names (usernameand passwordby default).

已解决:好的,我相信有几个原因导致我无法使其正常工作。序列化的东西可能是一个(我还没有测试过),但是既然 Jared 说需要它们,我就把它们留在里面(他是 Passport 的作者)。另一个混淆可能与表达版本和我对 npm 的混淆有关。我相信我测试了 express 的最新 v2,但我也测试了我现在正在运行的 v3。对于第 3 版,您可能也应该查看connect-flashGithub 上的模块,因为 Jared 示例中使用的一些“flash”内容已从 express v3 中移出(因此模块将其放回...)。最后,确保您发布使用正确的命名输入名称(usernamepassword默认情况下)。

回答by Jared Hanson

It looks to me like you're missing the necessary user serialization logic to establish a login session. If I add these two functions to the JavaScript code, it works:

在我看来,您缺少建立登录会话所需的用户序列化逻辑。如果我将这两个函数添加到 JavaScript 代码中,它会起作用:

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

passport.deserializeUser(function(obj, done) {
  done(null, obj);
});

You'll want to serialize the users according to your needs. Details are at the bottom of this page: http://passportjs.org/guide/configuration.html

您需要根据需要对用户进行序列化。详情在本页底部:http: //passportjs.org/guide/configuration.html

回答by MateodelNorte

The post variable names tend to be the biggest gotcha for people I see having trouble with the local password strategy. It should probably be big and bold in the documentation, and there should probably be config values to change them.

对于我认为本地密码策略有问题的人来说,后变量名称往往是最大的问题。它在文档中可能应该是大而粗的,并且可能应该有配置值来更改它们。

回答by user3691080

I have done succsessfully from this link http://danialk.github.io/blog/2013/02/23/authentication-using-passportjs/download sample code from https://github.com/DanialK/PassportJS-Authenticationlocation

我已经从这个链接成功完成了 http://danialk.github.io/blog/2013/02/23/authentication-using-passportjs/https://github.com/DanialK/PassportJS-Authentication位置下载示例代码

Only one change is required in routes.js change code

只需要在 routes.js 更改代码中进行一项更改

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

To ------------------------------

到 - - - - - - - - - - - - - - -

app.post("/login" ,passport.authenticate('local',{failureRedirect : "/login"}), function(req,res){ res.render('your home page here', {user : req.user });});