1.0.4 • Published 7 years ago

loginregisternodejs v1.0.4

Weekly downloads
5
License
ISC
Repository
-
Last release
7 years ago

Register-login-nodeJs

This is a simple but powerfull login register with nodejs. And using the bcrypt to in crypt the password.

Install

For installation you need to install from the terminal in place where you project is.

npm install loginregisternodejs

Explanations

The files are:

schema.js

All the magic for the Back-End is done there.

There i have the schema creator, find for the mongodb and the hash function for encrypt. The password and for check the password if is the correct.

bcrypt

Install the bcrypt

npm install --save bcrypt

so you can hash the password.

First you need to make the schema for you insert in the mongodb the username and the password. So ypu need to install to help us the mongoose

npm install --save mongoose

var Schema = mongoose.Schema;

var login = new Schema({

    username: String,
    password: String
});

//here make the model
var userinsert =  mongoose.model('usersinsert', login);

And now we need to hashOutput to insert hash the password. First we require: var SchemaLogin = require('./model/schema'); In the app.js:

app.post('/register', function (req, res, next) {

    var myPassword = req.body.password;
    var userName = req.body.username;

    SchemaLogin.hashOutput(myPassword,userName);


    res.send('<p>ok</p>' );

});

Next we need to hash our password:

 var hashOutput = function hash(myPassword, username) {
    bcrypt.hash(myPassword, saltRounds, function(err, hash) {
        // Store hash in your password DB on mongodb.
        var UsersInsert = userinsert({
            username : username,
            password : hash
        });
        UsersInsert.save(function (err) {
            if (err) throw err;
            console.log('person login validation '+ UsersInsert.username +' save');
        });
    });

};

And to check the hash password:

 var check = function (myPassword, hash) {
     bcrypt.compare(myPassword, hash, function(err, res) {
         // res == true
         console.log('check is :'+ res);

     });
     return true;
 };

And in the app.js we can check the password if it Matches: With a login.

app.post('/login',function (req, res, next) {
//    here i we make the check for the password
    var myPassword = req.body.password;
    var userName = req.body.username;
    
    
    SchemaLogin.userinsert.find({"username": userName}, function (err, obj) {
        if (err) throw err;
        // object of all the users
        console.log(obj[0].password);
        var hashFromdb = obj[0].password;
        var isTrue = SchemaLogin.check(myPassword, hashFromdb);
        
        if(isTrue) {
            res.send('<p>The password has been check ok. you are the </p>');
        }else {
            res.send('<p>The password is wronk</p>');
        }
    next();
    });



});
1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago