login-utils

Boiler plate code to handle login/registration of users

login-utils

login-utils is a very simple user authentication library. The library has a default User MangoDB model (using mongoose). Moreover, the encryption of the password is performed using bcrypt.

$ npm install login-utils

The api is very straightforward to use.

  var loginUtils = require("login-utils");
  loginUtils.setBcryptNumberOfRounds(15);
 
  //You can get the current number of rounds via 
  var nbOfRounds = loginUtils.getBcryptNumberOfRounds();
  console.log("Current bcrypt number of rounds is %d", nbOfRounds);
  var loginUtils = require("login-utils");
  var email = "kenshiro@some-email.com";
 
  loginUtils.findUserByEmail(email, function(erruser){
    if(err){
      console.log("An error occurred while trying to find user by email: %s", err);
    }else{
      console.log("User has been retrieved: [id=%s, first-name=%s, last-name=%s, user-name=%s" +
                  ", email=%s, hashed-password=%s, bcrypt-salt=%s]", user._id, user.firstName, user.lastName,
                  user.userName, user.email, user.password, user.salt);
    }
  });
 
  var loginUtils = require("login-utils");
  var userName = "kenshiro.hackuto";
 
  loginUtils.findUserByUserName(userName, function(erruser){
    if(err){
      console.log("An error occurred while trying to find user by user name: %s", err);
    }else{
      console.log("User has been retrieved: [id=%s, first-name=%s, last-name=%s, user-name=%s" +
                  ", email=%s, hashed-password=%s, bcrypt-salt=%s]", user._id, user.firstName, user.lastName,
                  user.userName, user.email, user.password, user.salt);
    }
  });
 
  var loginUtils = require("login-utils");
  var firstName = "kenshiro";
  var lastName = "hackuto";
 
  loginUtils.findUsersByFirstAndLastName(firstName, lastName, function(erruser){
    if(err){
      console.log("An error occurred while trying to find user by first and last name: %s", err);
    }else{
      console.log("User has been retrieved: [id=%s, first-name=%s, last-name=%s, user-name=%s" +
                  ", email=%s, hashed-password=%s, bcrypt-salt=%s]", user._id, user.firstName, user.lastName,
                  user.userName, user.email, user.password, user.salt);
    }
  });
 
   var loginUtils = require("login-utils");
   var password = "Hackuto-Shinken is invincible";
   var email = "kenshiro@hackuto-shinken-mail.com";
 
   loginUtils.loginCheck(email, password, function(errresponse){
     if(err){
      console.log("An error occurred when authenticating user: %s", err);
     }else if(!response.user){
      console.log(response.message);
     }else{
      var user = response.user;
      console.log("Successfully authenticated user: [id=%s, first-name=%s, last-name=%s, user-name=%s" +
                        ", email=%s, hashed-password=%s, bcrypt-salt=%s]", user._id, user.firstName, user.lastName, user.userName,
                        user.email, user.password, user.salt);
     }
   });
 
  var loginUtils = require("login-utils");
  var firstName = "kenshiro";
  var lastName = "hackuto";
  var password = "Hackuto-Shinken is invincible";
  var email = "kenshiro@hackuto-shinken-mail.com";
 
  loginUtils.createNewUser({
      firstName: firstName,
      lastName: lastName,
      userName: firstName + "." + lastName,
      password: password,
      email: email
    }, function(erruser){
      if(err){
        console.log("An error occurred creating user: %s", err);
      }else{
        console.log("Successfully created user: [id=%s, first-name=%s, last-name=%s, user-name=%s" +
                    ", email=%s, hashed-password=%s, bcrypt-salt=%s]", user._id, user.firstName, user.lastName, user.userName
                    user.email, user.password, user.salt);
      }
    });

Note that this will also create a user, if not user with a matching email address is found.

    var loginUtils = require("login-utils");
    var firstName = "kenshiro";
    var lastName = "hackuto";
    var password = "Hackuto-Shinken is invincible";
    var email = "kenshiro@hackuto-shinken-mail.com";
    var providerName = "Facebook";
    var providerUserId = "1234Fbk07";
    var displayName = "kenshiro-o";
 
    var socialMediaUserData = {
      firstName: firstName,
      lastName: lastName,
      userName: kenshiro-o,
      email: email,
      providerName: providerName,
      providerUserId: providerUserId,
      displayName: displayName;
    };
 
    loginUtils.createSocialMediaUser(socialMediaUserData, function(errsocialMediaUser){
      if(err){
          console.log("An error occurred when trying to create social media user [error=%s]", err);
      }else{
        var user = socialMediaUser.user;
        console.log("Successfully created a social media user [social-media-user-id=%s, provider-name=%s, provider-user-id=%s, display-name=%s]",
                     socialMediaUser._id, socialMediaUser.providerName, socialMediaUser.providerUserId, socialMediaUser.displayName);
 
        console.log("User associated with social media user is the following: [user-id=%s, first-name=%s, last-name=%s," +
                    "email=%s, hashed-password=%s, bcrypt-salt=%s]", user._id, user.firstName, user.lastName, user.email,
                    user.password, user.salt);
 
      }
    });
 
   var loginUtils = require("login-utils");
   var email = "kenshiro@hackuto-shinken-mail.com";
   //Token expires in 5 hours 
   var expiryInMillis = 1000 * 60 * 60 * 5;
   loginUtils.setPasswordResetTokenValidityInMillis(expiryInMillis);
 
   loginUtils.createPasswordResetToken(email, function(errtoken){
     if(err){
      console.log("An error occurred when trying to create password reset token for user: %s", err);
     } else{
        console.log("Successfully created a password reset token [user-id=%s, token-string=%s, token-expiry=%s]",
                    token.user, token.token, token.expiry);
     }
   });
 
   var loginUtils = require("login-utils");
   var tokenString = "GIBBERISH_)821832jkhhfdshdfhdls";
 
   loginUtils.findPasswordResetTokenByTokenString(tokenString, function(errtoken){
     if(err){
      console.log("An error occurred when trying to find password reset token by token string: %s", err);
     }else if(!token){
      console.log("No such password reset token exists [token-string=%s]", tokenString);
     }else{
        console.log("Found password reset token [user-id=%s, token-string=%s, token-expiry=%s]",
                    token.user, token.token, token.expiry);
     }
   });
 
  • Retrieving user by id
  • customizing bcrypt encrypter (e.g. configuring the number of rounds)

(The MIT License)

Copyright (c) 2013 Kenshiro <kenshiro@kenshiro.me>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.