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

changify

v1.0.4

Published

Handle db changes with verification

Downloads

328

Readme

changify

A module to watch and handle DB changes to a user that need verification, like emails or phone numbers.

This module is to be used with the Uhray Boilerplate.

Overview

This allows you to protect changes to user information before they are verified. The module is built for a pretty specific use-case, so there is not a ton of configuring. Maybe someday it will be more generic.

Basically, here is the order of operations:

  1. User requests change to a value.
  2. Changify stores the change with a specified code.
  3. If a request is made for that specified code to be used, the change will occur in the database.

To use it, you'll need to set up the connection that sends the code to the user (via email, text, etc) and prompts them to make the request referenced in point 3 above.

Usage

In the Uhray Boilerplate in the API initialization right after the turnkey setup. This File.

  var changify = require('changify');

  // ... other stuff goes on here ...

  // Turnkey setup stuff above ^^^

  // Configure changify
  changify.launch({
    mongoose: mongoose,
    model: resources.users.Model,
    router: app,
    redirect: '/#/account_settings'
  });
  changify.add('email', function(email, cb) {
    resources.users.Model.findOne({ email: email }, function(e, d) {
      cb(e || !d && tools.emailRegex(email));
    });
  }, changify.uuid(), tools.changeEmail);
  changify.add('cell', function(d, cb) {
    cb(Number.isFinite(d));
  }, changify.nums(5), tools.changeCell);


  // Crud launching below ...

JS API

# changify.launch(options)

Pass necessary configuration options to changfy.

  • mongoose - (required) - Pass the mongoose object. This is to ensure changify uses the same one.
  • model - (required) - Pass the Mongoose Users Model.
  • router - (required) - Pass the Express router.
  • redirect - (default: '/') - The required url after a change has been verified.

# changify.add(key, validate, keygen, codeHandler, onChange)

  • key - (required) - The key on the user object (e.g. 'email').
  • validate - (required) - A function to validate the new value is allows. It's called with (newValue, callback) where callback expects to be called with (valid, [value]), where valid is a boolean of whether it's valid or not and value can override the value.
  • keygen - (required) - A key generator. Allows you to specify what type of key you want. See below for some packaged with changify.
  • codeHandler - (required) - A function called when a change is made so you can email the user (or whatever you want to do). It's called with (code, newValue, user), where the code is the unique code, the newValue is what they're changing it to, and the user is this user's Mongoose model.
  • cors - (Optional) - This is an optional configuration to allow cors requests. If truthy and not an object, it creates a configuration for cors that allows all origins to request cross-origin and allows credentials to be stored. If the configuration is an object, this object will be passed as the options to the cors middleware.
  • onChange - (Optional) - Function to be called when the value is changed. It's called with: (userID, newValue, key, userObject).

# changify.uuid()

Returns a function that creates a uuid. Used as a keygen.

# changify.nums(n)

Returns a function that creates a random number n digits long. It avoids 1 and 0 to avoid confusion with I, L, and O.

URL Routes

Changify sets up two URL routes needed for it to work.

  • POST /changify - Stores a new change This expects as the body like this: { "key": "email", "value": "[email protected]"},

  • GET /changify/:code - Enacts the change If the code is valid, the change is taken into effect. Once a change is made, it will be redirected to the redirect url for this key. Alternatively, if you query /changify/:code?json=true it will respond with { "error": error, "data": true } where data is true if a change is made, otherwise false.

Note: stored changes are only valid for 24 hours.