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

user-basic

v0.1.0

Published

Basic User Functionality

Downloads

5

Readme

user-basic

Basic User Functionality. Register, Login, Password Reset, Email Confirmation, etc.

Instantiation

const mysql = require('mysql');
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'me',
  password: 'secret',
  database: 'my_db'
});

const userBasic = require('user-basic');

const dataModel = userBasic.dataModelMysql({
    table: 'user',
    connection: connection
});

const confirmationModel = userBasic.notificationModelEMail({
    from: '[email protected]',
    smtp: {
        host: 'mail.foo.com',
        port: 587,
        auth: {
            user: '[email protected]',
            pass: 'secret'
        }
    },
    getToField: fig => Q(fig.user.email),
    bodyTemplate: fig => Q('<h1>Hello</h1><pre>' + JSON.stringify(fig, null , 2) + '</pre>'),
    subjectTemplate: fig => Q('Please Confirm your Email: ' + fig.user.firstName)
});

const passwordResetModel = userBasic.notificationModelEMail({
    from: '[email protected]',
    smtp: {
        host: 'mail.foo.com',
        port: 587,
        auth: {
            user: '[email protected]',
            pass: 'secret'
        }
    },
    getToField: fig => Q(fig.user.email),
    bodyTemplate: fig => Q('<h1>Hello</h1><pre>' + JSON.stringify(fig, null , 2) + '</pre>'),
    subjectTemplate: fig => Q('Password Reset Requested: ' + fig.user.firstName)
});

const model = userBasic.model({
    dataModel: dataModel,
    // confirmationModel is optional
    confirmationModel: confirmationModel,
    // passwordResetModel is optional
    passwordResetModel: passwordResetModel,
    tokenSecret: 'secret',
    loginExpirationSeconds: 60 * 60,
    passwordResetExpirationSeconds: 60 * 5,
    confirmationExpirationSeconds: 60 * 60 * 24,
    // emailField is optional
    emailField: 'email'
});

!Note The emailField is optional. If you do not set an emailField, the model logic will assume there is no email. You can also set the emailField to "username", to give the username field the added responsibility of the user's email.

!Note If a confirmationModel is not supplied the sendConfirmation method will not work

!Note If a passwordResetModel is not supplied the sendPasswordReset method will not work


API

Register

model.register({
    username: 'bob',
    password: 'secret'
})
.then(() => {});

Login

model.login({
    usernameOrEmail: 'bob',
    password: 'secret'
})
.then(token => {});

Validate Login Token

model.validateLoginToken('eyjad...')
.then(decodedToken => {
    console.log(decodedToken.username);
});

Find by Username

model.findByUsername('bob')
.then(user => {});

Send Confirmation notification to User

model.sendConfirmation('bob')
.then(() => {});

Confirm with Token

Takes a confirmation token, and sets the user's account to confirmed.

model.confirmWithToken('abcde')
.then(() => {});

Send Password Reset request to User

model.sendPasswordReset('bob')
.then(() => {});

Reset Password with Token

Takes a password reset token, and new password, and resets the user's password.

model.resetPasswordWithToken({
    token: 'abcde',
    newPassword: 'new-password'
})
.then(() => {});

Data Model

A Data Model is dependency injected, to allow the library to be implemented against different storage mechanisms.

The library comes with a mysql data model, but you can implement your own (if you wanted to use some other database to store your users for example).

Your data model must implement the following methods:

insert

dataModel.insert({
    username: 'bob',
    password: 'abcdef'
})
.then(() => {});

findByField

Should handle the username field and the email field if it has been set during the model instantiation.

dataModel.findByField('fieldName', 'fieldValue')
.then(user => {});

setConfirmedByUsername

dataModel.setConfirmedByUsername({
    username: 'bob',
    isConfirmed: true
})
.then(() => {});

setPasswordByUsername

dataModel.setPasswordByUsername({
    username: 'bob',
    password: 'password'
})
.then(() => {});

Notification Model

The notification model is dependency injected as the confirmationModel and passwordResetModel. You can supply your own notification model (for example to send notifications by text message)

An email notification model is already supplied (see "instantiation" in these docs)

If you with to implement your own notification model you must implement the following interface

send

Send the user a message to confirm their account.

confirmationModel.send = fig => {
    // supplied token can be used by the "model.confirmWithToken" method
    console.log(fig.token);
    // the full user details are also supplied
    console.log(fig.user);
    // method should return a promise (reject promise is send is unsuccessful)
    return new Promise();
};