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

stateless-email-auth

v0.3.0

Published

stateless email authorization authentication for nodejs web applications

Downloads

11

Readme

stateless-email-auth

Stateless, passwordless email authentication in nodejs. Optionally uses json web tokens for stateless persistence. Designed to provide maximum security with minimal configuration.

Installation

npm install stateless-email-auth

Successful Authorization Flow

  • check if a user's email is on an authorization list
  • email that user an encrypted token in an email link
  • user clicks the link, which puts the token in a get request
  • website checks the token, if valid, issues a JWT and stores in a cookie
  • redirects to defined authentication success page

Basic Usage

Configuration

const auth = require('stateless-email-auth');

auth.config({
   users: [//array of authorized users for a static list, required unless checkUser is defined
      {email:'[email protected]', level: 'admin'},
      {email:'[email protected]', level: 'user'}
   ],
   checkUser: database.findEmail, //optional user-defined function to check email validity
   mailServer: 'mailserver.mail.com',  //required
   mailUser: '[email protected]',  //required
   mailSender: '[email protected]', //optional, defaults to mailUser
   mailPassword: 'jenny8675309password',  //required
   tokenUrl: 'http://localhost:3000/auth',  //required, full url to insert into email with generated token
   successPage: "/success", //optional, path to redirect successful authentication, will return 200 on sucess otherwise
   failPage: "/fail", // optional, path to redirect failed authentication, will return 403 otherwise
   cryptoKey: "crypt00_key", //required, will throw an error if you leave default key,
   mailServerPort: 587, //optional, defaults to 587
   mailServerSecurity: false, //optional, defaults to false
   mailSubject: "Email Verification", //optional
   tokenExpiration: 5, //optional, token expiration time in minutes, defaults to 5
   JWTexpiration: '14d', //optional
});

Send an authentication email

// will send an authentication email with an encrypted authorization token link if the email is valid
auth.sendToken('[email protected]');

Express Middleware to Check Auth Token

//sets JWT in cookie if valid
app.use('/authRoute/:token', auth.mw.checkToken);

Express Middleware to Check JWT

//checks JWT and sets req.user to the email and req.level to the user's auth level
app.use('/protectedRoute', auth.mw.checkJWT);

API

Check an authentication token

// user will be the email to which token was issued
var user = auth.checkToken(token);

Issue a json web token

//second argument (auth level) is optional, defaults to 'user'
var jwt = auth.getJWT('[email protected]', 'admin');

Check a json web token

//returns email and authorization level stored in JWT
var userinfo = await auth.checkJWT(jwt);

User-defined email checker

//this is a sample to adapt to your database schema

// must return or resolve an authorization level of some sort if valid
// must return or resolve false if invalid

function checkEmail(email){
   return new Promise(async (resolve,reject)=>{
      var user = await db.find({userEmail: email});
      if(user)
         resolve(user.authLevel);
      else
         resolve(false);
   });
}