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 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-passport-security

v0.9.1

Published

An opionated library to secure Express apps with Passport

Downloads

31

Readme

express-passport-security

Configures an Express app for authentication using Passport's local strategy.

Install

yarn add express-passport-security

API

configurePassport(findUser, findUserById)

Configures Passport with the required user retrieval functions.

  • findUser(username) returns a user from a given username for authentication

  • findUserById(id) returns a user from a given ID for session deserialisation

Note: the user returned must have an id property to uniquely identify the user and a password property containing a bcrypt hash of the user's password. Use something like node-password-util to correctly hash passwords when creating users.

configureApp(app, [options])

Integrates the given Express instance with Passport and adds a login and logout route handler.

  • app the Express instance

  • options optional object to allow configuration of post authentication flow. Available options are:

    • userDetailsExtractor(user) an optional function which returns only the required properties from the user object. By default, just the password will be removed.

    • loadInitialData(user, req) an optional function to return any initial data required by the app after successful authentication. Any implementation given must return a Promise which resolves to an object containing the initial data.

middleware()

Express middleware to redirect unauthenticated users in secure routes to the login page. returnTo is set on the session to allow redirection back to the route after login.

configureRoutes(login, logout)

By default, login and logout routes are set to /login and /logout respectively. Use this method to configure the routes.

Note: this will need to be called before configureApp and middleware.

Example

import express from 'express';
import body from 'body-parser';
import cookies from 'cookie-parser';
import session from 'express-session';
import {configureApp, configurePassport, middleware} from 'express-passport-security';

const app = express();
app.use(body.urlencoded({extended: true}));
app.use(cookies());
app.use(session({
  secret: 'secretsquirrel',
  saveUninitialized: true,
  resave: true
}));

// configure passport (findByEmail and findById implementations not shown)
configurePassport(findByEmail, findById);

// configure express
configureApp(app, {
  loadInitialData(user, req) {
    // return foo and bars after authentication (loadFoo and loadBars implementations not shown)
    return Promise.all([loadFoo(), loadBars()]).then(([foo, bars]) => {
      return {foo, bars};
    });
  }
});

// no authentication required so no middleware included
app.use('/', (req, res) => {
  res.send('Hello, World!');
});

// secure /foo with middleware
app.use('/foo', middleware, (req, res) => {
  res.send("I'm secure!");
});

Licence

MIT