如何使用 Node.js、Express 和 Mongoose 进行身份验证?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8397977/
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-09-02 14:51:47  来源:igfitidea点击:

How to do authentication with Node.js, Express and Mongoose?

authenticationnode.jsexpressmongoose

提问by Erik

I've made simple nodejs application by using nodejs+express. Now I want to make user authentification. I want to realize session handling by using mongoose. Can you advise some example?

我使用 nodejs+express 制作了简单的 nodejs 应用程序。现在我想进行用户身份验证。我想通过使用猫鼬来实现会话处理。你能提供一些例子吗?

采纳答案by alessioalex

Some useful links:

一些有用的链接:

how to implement login auth in node.js

如何在 node.js 中实现登录身份验证

creating registration and login form in node.js and mongodb

在 node.js 和 mongodb 中创建注册和登录表单

Also session management isn't done by Mongoose, but by connect-mongodb or connect-redis. You can checkout an example on how to do user auth and session management in a real application here:

此外,会话管理不是由 Mongoose 完成的,而是由 connect-mongodb 或 connect-redis 完成的。您可以在此处查看有关如何在实际应用程序中进行用户身份验证和会话管理的示例:

https://github.com/alexyoung/nodepad/blob/master/app.js

https://github.com/alexyoung/nodepad/blob/master/app.js

Further explanations for that app you can find here: http://dailyjs.com/tag/lmawaor http://dailyjs.com/2010/12/06/node-tutorial-5/

您可以在此处找到该应用程序的进一步说明:http: //dailyjs.com/tag/lmawahttp://dailyjs.com/2010/12/06/node-tutorial-5/

回答by alexandru.topliceanu

Just use mongoose-auth by Brian Noguchi https://github.com/bnoguchi/mongoose-auth

只需使用 Brian Noguchi https://github.com/bnoguchi/mongoose-auth 的mongoose-auth

It's a drop in solution for your question, it's well documented and extensible.

对于您的问题,这是一个简单的解决方案,它有据可查且可扩展。

EDIT

编辑

mongoose-authis no longer maintained. If you need to make it work with more recent versions of mongoose (ie > v3.x.x) and express (ie. > v3.x.x), here's an excerpt from a package.jsonfile I'm currently using in production (It's hacky but it works):

mongoose-auth不再维护。如果您需要使其与更新版本的 mongoose(即 > v3.xx)和 express(即 > v3.xx)一起使用,这里是package.json我目前在生产中使用的文件的摘录(它很笨,但它有效):

"everyauth": "https://github.com/bnoguchi/everyauth/tarball/express3",
"mongoose-3x-types": "~1.0.5",
"mongoose-auth": "https://github.com/cbou/mongoose-auth/tarball/everymodule-fix",

I you are starting a new projectdon't use mongoose-auth, instead try out passport. It offers the same functionality, it's very flexible, however it has a different api. It's part of the locomotiveMVC framework and as such it's actively maintained.

正在开始一个新项目,不要使用mongoose-auth,而是尝试通行证。它提供了相同的功能,非常灵活,但是它有不同的 api。它是机车MVC 框架的一部分,因此得到积极维护。

回答by David Oliveros

I've posted a complete example of a complete auth system using mongoose + expressjs on here, in case you want to take a look:

我在这里发布了一个使用 mongoose + expressjs 的完整身份验证系统的完整示例,以防您想看一看:

Simple login page in nodejs using express and passport with mongodb

nodejs中使用express和passport以及mongodb的简单登录页面