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

jwt-mongo-sms

v2.4.2

Published

Authentication using JSON web tokens, Mongo DB, and Twilio SMS

Downloads

14

Readme

jwt-mongo-sms

If you're wondering how to implement authentication with JSON web tokens, Mongo DB, Twilio SMS, and (optionally) GraphQL, you're in the right place!

Installation

npm install jwt-mongo-sms

or

yarn add jwt-mongo-sms

Quickstart

(Before doing anything with this package, you'll need a Mongo database and a Twilio account with a phone number to use for SMS.)

Create an instance of JwtMongoSms:

import JwtMongoSms from 'jwt-mongo-sms';

const {
  JWT_SECRET,
  MONGODB_URI,
  TWILIO_ACCOUNT_SID,
  TWILIO_AUTH_TOKEN,
  TWILIO_SMS_PHONE_NUMBER,
} = process.env;

const jwtMongoSms = new JwtMongoSms({
  jwtSecret: JWT_SECRET,
  mongoUri: MONGODB_URI,
  twilio: {
    accountSid: TWILIO_ACCOUNT_SID,
    authToken: TWILIO_AUTH_TOKEN,
    phoneNumber: TWILIO_SMS_PHONE_NUMBER,
  },
});

export default jwtMongoSms;

Set up REST endpoints to send and verify authentication code (for GraphQL, see Examples below):

import bodyParser from 'body-parser';
import express from 'express';
import jwtMongoSms from './jwtMongoSms'; // from wherever you instantiated JwtMongoSms

const app = express();

// Parses request body as object
app.use(bodyParser.urlencoded());

// Sample route that sends authentication code via SMS
app.post('/send-auth-code', (request, response) => {
  const { phoneNumber } = request.body;

  // This is a good spot to create your own user with "phoneNumber" if they don't already exist
  // Alternatively, if your user collection and auth collection are the same, the function below
  // will upsert a user/auth document with "phoneNumber", "authCode", and "authCodeCreatedAt"

  jwtMongoSms.sendAuthCode(phoneNumber);

  response.end();
});

// Sample route that verifies authentication code (on success, returns a JSON web token and user data)
app.post('/verify-auth-code', async (request, response) => {
  const { phoneNumber, authCode } = request.body;
  const { authToken, user } = await jwtMongoSms.verifyAuthCode({ phoneNumber, authCode });

  response.json({ authToken, user });
});

// Authenticates each server request by checking for a JSON web token in the "Authorization" header
app.use(jwtMongoSms.getAuthMiddleware());

// Sample authenticated route
app.get('/profile', (request, response) => {
  if (request.user) {
    res.status(200).send('Authenticated!');
  } else {
    res.status(401).send('Unauthorized!');
  }
});

Once an authentication code is verified, the client will receive a JSON web token. To make authenticated requests, you will need to store this token on the client (using localStorage, cookies, or any other preferred method) and send it via the request Authorization header. If your JSON web token were abc123, for instance, the header value would be JWT abc123. (For more details, see the GraphQL example below.)

Finally, with the middleware in place, you can check request.user in any subsequent server request to determine which user (if any) has been authenticated!

Examples

GraphQL

Configuration

There are three required fields when instantiating a JwtMongoSms object: jwtSecret, mongoUri, and twilio. Configuring the rest is optional.

Field|Default Value|Description ---|---|--- jwtSecret||JSON web token secret mongoUri||Mongo URI (e.g., mongodb://localhost/my-db) twilio|{}|Twilio credentials (accountSid, authToken) and phoneNumber used to send SMS text setSmsMessage|(authCode => `Your authentication code is ${authCode}`)|Used to set the message for SMS authentication usersCollectionName|users|Name of the Mongo collection used to store user data authCollectionName|users|Name of the Mongo collection used to store auth data requestKey|user|Key your authenticated user will be assigned to on each server request authCodeLength|4|Length of authentication code authCodeTimeoutSeconds|600|Number of seconds it takes for a authentication code to expire decodeUserId|(userId => ObjectId.createFromHexString(userId))|Determines the format of _id for the auth middleware user query. If your user ids are stored as strings instead of ObjectIds (e.g., Meteor), you should replace this with (userId) => userId) whitelistedPhoneNumber||Use this phone number for testing a "happy path". No text will be sent via sendAuthCode (so fake phone numbers will work), and any auth code submitted via verifyAuthCode will be considered a match

API

The following are methods from the JwtMongoSms class you can use (from an instantiated object):

getAuthMiddleware() : express.Handler[]
  • Returns the middleware needed for authenticating server requests.
sendAuthCode(phoneNumber: string) : Promise<boolean>
  • Sends authentication code via Twilio SMS. Upserts auth collection document for phoneNumber with new authCode and authCodeCreatedAt. NOTE: By default userCollectionName and authCollectionName are both set to users. That means if you don't override these settings, this method will insert a user document for you (if it doesn't already exist). To avoid this behavior, be sure to create the user document beforehand.
verifyAuthCode({ phoneNumber: string, authCode: string }) : Promise<{ user: Object, authToken: string }>
  • Verifies inputted authentication code. Will throw errors if no auth data is found, no code has been generated, the compared codes do not match, or if the code has expired. When verified, the found user document and a generated authToken are returned.
createAuthIndex(fieldOrSpec: string = 'phoneNumber', IndexOptions: object = { unique: true }): Promise<string>
createUsersIndex(fieldOrSpec: string = 'phoneNumber', IndexOptions: object = { unique: true }): Promise<string>
  • Indexes the auth and users collections respectively. Each defaults to a unique index on phoneNumber for faster lookup and data integrity. See the MongoDB documentation for more info on fieldOrSpec and options.

Issues

If you experience bugs, try upgrading to the latest version and checking the changelog. You can also post an issue on GitHub!