Creating a registration form and registration in node.js and mongodb

I am new to node.js and want to create a login and login page for the user. In addition, the user requires the correct authorization. I want to save user information in mongodb database. How can I achieve this. Show someone the code for this so that I can get started with node.js and mongodb. Please help

+38
mongodb express
Nov 08 '11 at 2:05 a.m.
source share
3 answers

You can find a complete sample of what you are trying to do in Alex Young's Nodepad app. The 2 important files you should pay attention to are the following 2:

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

Part of the model is as follows:

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 dailyjs website .

+42
Nov 13 '11 at 12:53
source share

I wrote a template project to do just that. It supports account creation, email password recovery, sessions, local cookies to remember users when they return and encrypt passwords through bcyrpt.

There is also a detailed explanation of the project architecture on my blog.

+21
Jun 13 '12 at 17:56
source share

For an easy start, take a look at ExpressJS + MongooseJS + MongooseAuth .

In particular, this last plugin provides a standard easy way to log in using several different authentication methods (Password, Facebook, Twitter, etc.).

+10
Nov 08 2018-11-11T00:
source share



All Articles