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

highoutput-auth

v3.0.1

Published

Authentication and authorization module

Downloads

19

Readme

Auth

new Auth(options)

The Auth constructor accepts the following configuration

  • options.secretKey string Secret key to use in generating the JWT.
  • options.model model User model.
import mongoose, { Schema } from 'mongoose';
import Auth from 'highoutput-auth';

const AccountModel = {
  async findByUsername(username) => { ... },

  async findById(id) => { ... },

  async updatePassword(id, password) => { ... },
};

const auth = new Auth({
  secretKey: '4fb473f82ba47bf6acbab33e7529fb96',
  model: AccountModel
});

auth.createAccessToken(params)

Create an access token.

  • params.username string Username.
  • params.password string Password.
  • params.expiresIn string (Optional) Amount of time before the accessToken expires. Must be compatible to the ms package.
  • params.claims object (Optional) Additional claims to include in the JWT.
  • Returns: accessToken Promise<string>
  • Throws:
    • INVALID_CREDENTIALS
await auth.createAccessToken({
  username: 'roger',
  password: '123456Seven',
});

auth.verifyAccessToken(params)

Verify a json web token.

  • params.accessToken string Access token.
  • params.subject string | number (Optional) ID of the owner of the accessToken.
  • Returns: claims Promise<object> Claims stored in the JWT.
  • Throws:
    • INVALID_TOKEN
await auth.verifyAccessToken({
  accessToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1YWQ3MTZlZjc1ZTZhODc1MTQ0Y2Q0NDQiLCJpYXQiOjE1MjQ2MjYzNjMsImV4cCI6MTUyNTIzMTE2M30.z2xgs0BeLQsTBiG9sphjkP_JljYht2o4AgI4ClWgZqw',
});

auth.changePassword(params)

Change the user password.

  • params.accessToken string Access token.
  • params.subject string | number (Optional) ID of the owner of the accessToken.
  • params.oldPassword string Old password.
  • params.newPassword string New Password.
  • Throws:
    • INVALID_TOKEN
    • INVALID_CREDENTIALS
await auth.changePassword({
  accessToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1YWQ3MTZlZjc1ZTZhODc1MTQ0Y2Q0NDQiLCJpYXQiOjE1MjQ2MjYzNjMsImV4cCI6MTUyNTIzMTE2M30.z2xgs0BeLQsTBiG9sphjkP_JljYht2o4AgI4ClWgZqw',
  oldPassword: 'password',
  newPassword: '123456Seven',
});

auth.requestResetPassword(params)

Request for a password reset.

  • params.subject string | number ID of the user requesting the password reset.
  • params.expiresIn string (Optional) Amount of time before the requestToken expires.
  • Returns: requestToken Promise<string>
  • Throws:
    • USER_NOT_FOUND
await auth.requestResetPassword({
  subject: '507f1f77bcf86cd799439011',
});

auth.resetPassword(params)

Reset password.

  • params.requestToken string Request token.
  • params.subject string | number (Optional) ID of the owner of the requestToken.
  • params.password string New Password.
  • Throws:
    • INVALID_TOKEN
await auth.resetPassword({
  requestToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1YWQ3MTZlZjc1ZTZhODc1MTQ0Y2Q0NDQiLCJpYXQiOjE1MjQ2MjYzNjMsImV4cCI6MTUyNTIzMTE2M30.z2xgs0BeLQsTBiG9sphjkP_JljYht2o4AgI4ClWgZqw',
  password: '123456Seven',
});

Auth Model

The model should have the following methods

  • findByUsername(username) Promise<{ id: string | number, username: string, password: string }>
  • findById(id) Promise<{ id: string | number, username: string, password: string }>
  • updatePassword(id, password) Promise<void>
const { bcrypt } = require('highoutput-auth');
const R = require('ramda');

class Model {
  constructor() {
    this.users = [];
  }

  /* additional method for the sake of context */
  async insertUser(user) {
    this.users.push({
      ...user,
      password: await bcrypt.hash(user.password),
    });
  }

  async findByUsername(username) {
    return R.find(R.propEq('username', username))(this.users);
  }

  async findById(id) {
    return R.find(R.propEq('id', id))(this.users);
  }

  async updatePassword(id, password) {
    const user = R.find(R.propEq('id', id))(this.users);

    if (!user) {
      return;
    }

    user.password = password;
  }
}