在node.js和mongodb中创build注册和login表单

我是node.js的新手,想为user.also创build一个注册和login页面,并且需要为用户进行适当的授权。我想在mongodb数据库中存储用户信息。我可以如何实现这个function。这样做的代码,以便我可以开始与node.js和mongodb.Please帮助

您可以在Alex Young的Nodepad应用程序中find完整的示例。 这2个重要的文件你应该看看这些2:

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

模型的一部分是这样的:

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(); } }); 

我想他也解释了dailyjs网站上的代码 。

我写了一个样板工程来做到这一点。 它支持帐户创build,通过电子邮件进行密码检索,会话,返回时记住用户的本地cookie以及通过bcyrpt安全密码encryption。

我的博客上也有关于项目架构的详细解释。

要获得一个简单的方法,请查看ExpressJS + MongooseJS + MongooseAuth 。

特别是,最后一个插件提供了使用几种不同的身份validation方法(密码,Facebook,Twitter等)进行login的标准简单方法。