npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

multi-auth

v0.0.4

Published

An authentication module that supports passwords, email auth, and 2-step auth

Readme

multi-auth

An agnostic node.js module for multiple authentication related tasks.

  • Simple password authentication
  • Two-step authentication (password + email)
  • Email-only authentication
  • Email confirmation
  • Password resets by email

Installation

$ npm install multi-auth

Usage

Setup

Setting up the auth controller consists of defining functions to tell the module how to perform certain tasks (like sending an email or looking up a user from the database).

// Load in the auth module
var Auth = require('multi-auth');

// Create the auth controller
var auth = new Auth();

//
// Tell multi-auth how to send emails
//
auth.define('sendEmail', function(user, template, data, promise) {
	var mail = mailer.sendEmail({
		to: user.email,
		template: template,
		data: data
	});

	mail.on('sent', function() {
		promise.resolve();
	});
});

//
// Tell multi-auth how to fetch users from the database using a user ID
//
auth.define('fetchUserById', function(userId, promise) {
	User.findById(userId, function(err, user) {
		if (err) {
			return promise.reject(err);
		}
		
		promise.resolve({
			id: user._id,
			authMethod: user.authMethod,
			email: user.email,  // if needed (twostep-email and email)
			password: user.password,  // if needed (password, twostep-sms, and twostep-email)
			phone: user.phone  // if needed (twostep-sms)
		});
	});
});

//
// Tell multi-auth how to fetch users from the database using a username/email
//
auth.define('fetchUserByName', function(userName, promise) {
	var query = { };
	
	if (userName.indexOf('@') >= 0) {
		query.email = userName;
	} else {
		query.username = userName;
	}

	User.findOne(query, function(err, user) {
		if (err) {
			return promise.reject(err);
		}

		promise.resolve({
			id: user._id,
			authMethod: user.authMethod,
			email: user.email,  // if needed (twostep-email and email)
			password: user.password,  // if needed (password, twostep-sms, and twostep-email)
			phone: user.phone  // if needed (twostep-sms)
		});
	});
});

//
// Tell multi-auth how to test passwords
//
auth.define('checkPassword', function(user, password, promise) {
	promise.resolve(hashPassword(password) === user.password);
});

//
// Tell multi-auth how to mark emails as confirmed
//
auth.define('confirmEmail', function(userId, promise) {
	User.findById(userId, function(err, user) {
		if (err) {
			return promise.reject(err);
		}

		user.emailConfirmed = true;
		user.save(function() {
			promise.resolve();
		});
	});
});

//
// Tell multi-auth how to update passwords
//
auth.define('updatePassword', function(userId, promise) {
	User.findById(userId, function(err, user) {
		if (err) {
			return promise.reject(err);
		}

		user.emailConfirmed = true;
		user.save(function() {
			promise.resolve();
		});
	});
});

Express Middleware

If you use express, you can use the middleware to handle all router setup. Just use it right before using the router.

var app = express();

// other middlewares ....

app.use(auth.express({
	route: '/auth',
	afterAuth: function(user, req, res) {
		res.send(200, {
			message: 'Authentication successful',
			token: generateTokenForUser(user)
		});
	}
}));
app.use(app.router);

// any error handlers ....

app.listen(somePort, function() {
	console.log('express app running ...');
});

Defining Routes

If you're not using express, you will have to define your routes yourself. Don't worry, though, the exposed methods are very straight-forward and easy to use.