在 node.js 和 mongodb 中创建注册和登录表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8051631/
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
creating registration and login form in node.js and mongodb
提问by Dar Hamid
I am new to node.js and want to create a registration and login page for user.also there has to proper authorisation for the user.I want to store the user information inside mongodb database.How can i achieve this.can someone provide me the code to do so, so that i can get started with node.js and mongodb.Please help
我是 node.js 的新手,想为用户创建一个注册和登录页面。还必须对用户进行适当的授权。我想将用户信息存储在 mongodb 数据库中。我怎样才能做到这一点。有人可以提供我吗这样做的代码,以便我可以开始使用 node.js 和 mongodb。请帮忙
回答by alessioalex
You can find a complete sample of what you're trying to do in the Nodepad application by Alex Young. The 2 important file you should take a look at are these 2:
https://github.com/alexyoung/nodepad/blob/master/models.js
https://github.com/alexyoung/nodepad/blob/master/app.js
您可以在Alex Young的Nodepad 应用程序中找到您尝试执行的操作的完整示例。您应该查看的 2 个重要文件是以下 2 个:
https: //github.com/alexyoung/nodepad/blob/master/models.js
https://github.com/alexyoung/nodepad/blob/master/app .js
A part of the model look like this:
模型的一部分如下所示:
User = new Schema({
'email': { type: String, validate: [validatePresenceOf, 'an email is required'], index: { unique: true } },
'hashed_password': String,
'salt': String
});
User.virtual('id')
.get(function() {
return this._id.toHexString();
});
User.virtual('password')
.set(function(password) {
this._password = password;
this.salt = this.makeSalt();
this.hashed_password = this.encryptPassword(password);
})
.get(function() { return this._password; });
User.method('authenticate', function(plainText) {
return this.encryptPassword(plainText) === this.hashed_password;
});
User.method('makeSalt', function() {
return Math.round((new Date().valueOf() * Math.random())) + '';
});
User.method('encryptPassword', function(password) {
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
});
User.pre('save', function(next) {
if (!validatePresenceOf(this.password)) {
next(new Error('Invalid password'));
} else {
next();
}
});
I think he also explains the code on the dailyjs site.
我想他也在dailyjs 网站上解释了代码。
回答by braitsch
I wrote a boilerplate projectto do exactly this.It supports account creation, password retrieval via email, sessions, local cookies for remembering users when they return and secure password encryption via bcyrpt.
我写了一个样板项目来做到这一点。它支持帐户创建、通过电子邮件检索密码、会话、本地 cookie 以在用户返回时记住用户并通过 bcyrpt 进行安全密码加密。
There's also a detailed explanation of the project's architecture on my blog.
回答by Gates VP
For an easy way to get started take a look at ExpressJS+ MongooseJS+ MongooseAuth.
有关入门的简单方法,请查看ExpressJS+ MongooseJS+ MongooseAuth。
In particular, that last plug-in provides a standard simple way to do logins using several different authentication methods (Password, Facebook, Twitter, etc.)
特别是,最后一个插件提供了一种标准的简单方法来使用几种不同的身份验证方法(密码、Facebook、Twitter 等)进行登录。

