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-persona-observer

v0.0.2

Published

Opinionated Mozilla Persona Observer API authentication for your Express application

Downloads

8

Readme

express-persona-observer

Opinionated Mozilla Persona integration for Express. express-persona-observer adds functionality to express-persona to make integration of Persona using its Observer API even more seamless.

Quick start

Install using npm: npm install express-persona-observer

Include the module inside your Express application:

var express = require("express");
var persona = require("express-persona-observer");
var app = express();

app.use(express.json());
app.use(express.urlencoded());
app.use(express.cookieParser());
app.use(express.session({
  secret: "mozillapersonaiswatchingyou"
}));

persona.express(app, {
  audience: "http://localhost:8888" // Must match your browser's address bar
});

Include the Persona library and login script in your web pages:

<script src="https://login.persona.org/include.js"></script>
<script src="/persona/login.js"></script>

or

<script src="https://login.persona.org/include.js"></script>
<script src="{{loginScriptUrl}}"></script>

if you're using a templating engine.

Add login and logout buttons to your page:

<button id="login">Log In</button>
<button id="logout">Log Out</button>

Like express-persona, by default the user's email address is added to req.session.email when their email is validated.

You can view and run a complete example in the examples directory.

Documentation

express-persona-observer provides both the server and client-side code to integrate Persona into your express application, with sensible defaults so it works right out of the box. Additionally, it provides several useful route middleware methods, request methods, and application locals to take the pain out of writing Persona-based applications.

Route middleware

  • ensureLoggedIn([path]) - errors or redirects if a user is not logged in
    • path is an optional string specifying a redirect path; if omitted next() will be called with an error unless a default redirect path has been specified as an option to express()
  • ensureLoggedOut([path]) - errors or redirects if a user is logged in
    • path is an optional string specifying a redirect path; if omitted next() will be called with an error unless a default redirect path has been specified as an option to express()

Request helpers

  • fromLoggedInUser - returns true if the request session includes a user

Templating locals

  • loggedInUser - logged in user email, or null
  • loginScriptUrl - path to login.js

Configuration

  • express(app, options)
    • app is an instance of the express server that you want to add routes to.
    • options is an object. It has one required parameter, audience.

Required options

  • audience - The URL of your express app when viewed in a browser. Must include the protocol, hostname, and port.
    • Example: http://example.org:80, https://example.org:443

Optional options

  • express-persona-observer supports all express-persona options.
  • syncResponse(req, res, next) - Response handler when your app needs to synchronize its session with Persona.
    • Default: none
    • req, res, next are the typical express middleware callback arguments
    • The provided handler should return a page that loads login.js, which handles synchronization with Persona. The path will then be reloaded automatically.
  • loginjsPath - Path at which login.js will be hosted.
    • Default: /persona/login.js
  • redirects - object containing default redirects for route middleware methods
    • notLoggedIn - string specifying default redirect path for ensureLoggedIn
    • notLoggedOut - string specifying default redirect path for ensureLoggedOut
  • exemptPaths - an array of strings specifying paths that are exempt from synchronization
  • selectors - object containing login and logout button selectors
    • login - string specifying login button selector, defaults to #login
    • logout - string specifying logout button selector, defaults to #logout

Tests

Tests can be run with npm test. Test coverage can be generated with node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -R spec test/*.test.js.