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

sa-express

v3.1.1

Published

Scratch Auth integration for Express. Just plug and play, minimal setup required

Downloads

33

Readme

Scratch Auth integration for Express

  • Plug and play, minimal setup required
  • Easy to use

Installation

npm install sa-express

Usage

const express = require('express');
const scratchauth = require('sa-express');

const app = express();

const needsAuth = scratchauth(app, {
  secret: 'SuperSecret1234',
  appName: 'My Cool Express App',
  succeeded(req, res) {
    res.redirect('/welcome');
  },
  failed(req, res) {
    res.redirect('/authfailed');
  },
});

Options

| Name | Description | Default | | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------- | | secret | Secret that cookie-session will use. It should be stored securely in an environment variable. | No default; this option is required | | appName | Name for Scratch Auth to use on the login page. | '' | | loginRoute | Route for redirecting the user to Scratch Auth. | '/auth/login' | | verifyRoute | Route for verifying Scratch Auth's repsonse. | '/auth/verify' | | logoutRoute | Route for logging the user out. | '/auth/logout' | | logoutRedirect | Route to redirect to after logging out. | '/' | | domain | The domain of your app. This is only needed if your app unexpectedly redirects to localhost instead of your app's domain. It should not include http[s]:// or a trailing slash. | '' | | succeeded | Called when the user has been logged in successfully. | (req, res) => res.redirect('/') | | failed | Called when auth has failed. | (req, res) => res.send('Auth failed') | | cookie | More options here. | By default lasts 7 days with sameSite: lax. |

Using Auth/Protected Routes

Calling scratchauth returns a middleware for protected routes. It will redirect the user to the route given if they are not logged in. By default, the route is whatever you passed for loginRoute.

app.get('/dashboard', needsAuth(), (req, res) => {
  res.send(`Welcome to your dashboard, ${res.locals.username}!`);
});

You can manually implement protected routes by using res.locals.loggedIn:

app.get('/dashboard', (req, res) => {
  if (res.locals.loggedIn) {
    res.send(`Welcome to your dashboard, ${res.locals.username}!`);
  } else {
    res.redirect('/auth/login');
  }
});

In fact, needsAuth uses res.locals.loggedIn under the hood, so both of the methods are exactly equivalent.

Demo Application

A demo can be found in demo/.