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

@frappy/js-mongo-user-store

v1.4.0

Published

MongoDB store classes for Users and UserTokens

Downloads

2

Readme

MongoDB User Stores for NodeJS

MongoDB Store Implementation of Users and UserTokens for NodeJS

Usage

import { UserStore, UserTokenStore } from "@frappy/js-mongo-user-store"
import mongodb from "mongodb"

const MONGO_URL = process.env.MONGO_URL || "mongodb://localhost:27017"
// create mongoDB connection
mongodb.MongoClient.connect(MONGO_URL, {
    useNewUrlParser: true,
}).then(client => {
    // initialise store
    const userStore = new UserStore(client, "myDatabaseName", "users")
    const userTokenStore = new UserTokenStore(client, "myDatabaseName", "userTokens")
    
    userStore.login("myusername", "mypassword").then(loggedInUser => { 
        if (loggedInUser == null) {
            console.log("Login Failed")
        }
    })
    userTokenStore.removeExpired(new Date().getTime() - 24 * 60 * 60 * 1000)
})

Methods

User Store

  • login(username, password) - local login attempt, will md5 encode the provided password and try to match against the database. This will either return null, if the login failed or the user if it succeeded.
  • getByUsername(username) - returns a single user or null that matches the requested username (local user).
  • getByUid(uid) - returns a single user or null that matches the requested uid attribute.
  • updatePermissions(userId, permissions) - updates the permissions array of a given user (overwrite)
  • updateProfile(userId, updateProfile) - updates the profile key of a user. This is not an officially supported key, but offers to store additional information with the user relevant to your application.
  • changePassword(userId, newPassword) - this method will update a given user's password. The password will be md5 encrypted before being stored.
  • initialLocalUserCheck(initialUsername, initialPassword, initialPermissions = ["admin"]) - this method is supposed to be used for applications using local users on startup. The method will check whether there are any users in the system at all. If there are no users available, it will create a user with the provided credentials and permissions.
  • createLocalUser(username, password, permissions = []) - creates a new local user. The password will be md5 encrypted before being stored. Also, the username has to be unique.
  • createApiKey(userId) - creates a new API key (16 alphanumeric) for the user provided by the user id and throws an error if the user cannot be found. Will return the newly updated API key as string when the promise resolves.
  • revokeApiKey(userId) - removes the API key for a user
  • getUsersWithApiKey

User Token Store

  • removeExpired(maxCreationDate) - removes any token that has been created before the max creation date. maxCreationDate has to be a Unix timestamp in milliseconds.
  • storeToken(newToken, user) - will store a new token associated to the given user. The user parameter has to be a user object with an _id attribute.
  • removeToken(token) - simply removes a specific token from the database.
  • getUsersWithApiKey(paging) - returns by default up to 100 users with an existing API key. This method should only be used for administrative functions. The paging parameter behaves the same as for find(..), where the paging is a JSON object with page and pageSize key/values. Pages start with 0.