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

@laurenskling/keystone-forgotten-password

v3.0.1

Published

# Keystone Forgotten Password

Downloads

12

Readme

Fork of Keystone Forgotten Password to update bcrypt

Keystone Forgotten Password

Build Status

What is This?

This is for keystone applications only. Keystone projects having a user model may require a reset password setup. this plugin adds the required models and routes, you will have to interact with the routes yourself in your own application, including writing your own email handlers.

Note

This plugin assumes you have a user model with a password property with a keystone Password field type.

Exports

	// Exposes routes for a password reset
	const forgotPassword = require('keystone-forgotten-password');

	// Exposes a single route to change a password for a logged in User.
	const { updatePassword } = require('keystone-forgotten-password');

Prerequisites

  • Node V6+
  • Keystone 4.0.0.beta-5
  • User model with Password field type
  • The need to add a password reset to your application

Usage

For IP logging of requests ensure you set:

	app.enable('trust proxy')

Forgotten Password


// routes/index.js
const forgottenPasswordPlugin = require('keystone-forgotten-password');

const forgottenPassword = forgottenPasswordPlugin({
	// define what happens on the given email handlers.
	onForgotEmail: (locals) => sendForgotEmail(locals),
	onChangePasswordEmail: (locals) => sendChangePasswordEmail(locals),
});

exports = module.exports = function (app) {
  app.get('/', routes.views.index);
  app.use('/auth/', forgottenPassword); // routes are mounted on /auth/ auth/forgot, auth/change-password will be added
};

// model/User.js
const keystone = require('keystone');
const { enhanceUserModel } = require('keystone-forgotten-password');

const User = new keystone.List('User');

User.add({
	email: { type: keystone.Field.Types.Email },
	password: { type: keystone.Field.Types.Password },
});

enhanceUserModel(User);

User.schema.virtual('canAccessKeystone').get(function () {
	return true;
});

User.defaultColumns = 'displayName, email';
User.register();

Update Password for Logged in Users

In the case where you have a 'Profile' page in your application and you want to allow user to change their password setup the following.


// routes/index.js
const { updatePassword } = require('keystone-forgotten-password');


exports = module.exports = function (app) {
  app.get('/', routes.views.index);
  app.use(middleware.requireUser, updatePassword()); // route is added to /update-password
  // alternatively
  app.use('/profile', middleware.requireUser, updatePassword());

  // forgottenPassword checks by default req.user from your custom middleware, want to check a different property i.e. req.appUser?
  // want to send an email?
  app.use('/profile', middleware.requireUser, updatePassword({
  	userRequest: 'appUser',
  	onChangePasswordEmail: (locals) => sendChangePasswordEmail(locals),
  }));

};

Routes

| Route | Payload | Response | |-----|--------|----------| | POST /forgot | { "email": "[email protected]" } | 400 for email validation, 200 for email exists or does not| | POST | /change-password | { "password": "usersNewPassword123", forgotPasswordKey: "(UNIQUE GUID Value)" }| | POST | /update-password (ensure this is behind auth middleware) | {"password": "usersNewPassword123", "existingPassword": "existingPassword" } |

API

Forgotten Password Plugin

const forgottenPasswordPlugin = require('keystone-forgotten-password');

accepts the following config object.

| Key | Value | Required | |-----|------------|----------| | onForgotEmail: Function | requires a function which returns a promise. The promise is given the entire user object and forgotPasswordKey which must be provided in the reset password link to change-password in your front end application. | Yes | | onChangePasswordEmail: Function | requires a function which returns a promise. The promise is given the entire user object | Yes | | keyExpiry: number | Integer in hours, defaults to 24 hours. The key sent in the email will live until the given expiry | No |

const { enhanceUser } = require('keystone-forgotten-password');

To add the additional property passwordLastUpdated: { type: Date }, to your User model you can use this helper. Adding this field manually is possibly but not recommended.

Update Password Plugin

// Exposes a single route to change a password for a logged in User.
const { updatePassword } = require('keystone-forgotten-password');

| Key | Value | Required | |-----|------------|----------| | onChangePasswordEmail: Function | requires a function which returns a promise. The promise is given the entire user object | No | | userRequest: string | name of property on express req object containing the current user defaults to req.user | No |