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

roadwork-authentication

v0.3.2

Published

Adds authentication to the roadwork library

Downloads

2

Readme

Roadwork Authentication

General

This library adds Authentication supports to the Roadwork library. Note that this will automatically add 2 tables to your database called user and user_session:

CREATE TABLE user
(
    id INT(10) unsigned PRIMARY KEY NOT NULL,
    email VARCHAR(255) NOT NULL,
    password VARCHAR(255),
    first_name VARCHAR(255) NOT NULL,
    middle_name VARCHAR(255) DEFAULT '',
    last_name VARCHAR(255) DEFAULT '',
    scope VARCHAR(255) DEFAULT 'user' NOT NULL,
    avatar_url VARCHAR(255) DEFAULT '/images/avatar.png' NOT NULL,
    email_verified TINYINT(1) DEFAULT '0' NOT NULL COMMENT 'Is the user verified and can he/she login?',
    email_verify_key VARCHAR(255) DEFAULT '' NOT NULL,
    email_date_sent VARCHAR(255) DEFAULT '0000-00-00 00:00:00' NOT NULL,
    forgot_password_token VARCHAR(255),
    created_at DATETIME DEFAULT 'CURRENT_TIMESTAMP' NOT NULL,
    updated_at DATETIME
);

CREATE TABLE user_session
(
    id INT(10) unsigned PRIMARY KEY NOT NULL,
    user_id INT(10) unsigned NOT NULL,
    token VARCHAR(255) NOT NULL,
    user_agent TEXT,
    ip VARCHAR(255),
    created_at DATETIME DEFAULT 'CURRENT_TIMESTAMP' NOT NULL,
    updated_at DATETIME,
    CONSTRAINT user_session_user_id_foreign FOREIGN KEY (user_id) REFERENCES user (id)
);
CREATE UNIQUE INDEX user_session_token_unique ON user_session (token);
CREATE INDEX user_session_user_id_foreign ON user_session (user_id);

The reason that it does this is so that we are able to provide the following features:

  • Add ACL to the main roadwork library
    • Allows us to specify which roles can access which routes
    • Adds the $owner dynamic role which only allows access to a route if the user owns the resource behind it
  • Add bearerAuthentication so that we can login with a session token and access a route if a role is specified

Usage

  1. npm install roadwork-authentication --save
  2. Install the database connector:
npm install pg --save
npm install mysql --save
npm install mariasql --save
npm install sqlite3 --save
  1. Enable authentication in the main roadwork library: roadwork.addAuthentication(require('roadwork-authentication'), dbConfig)

Note: the addAuthentication call is a promise that will be resolved. This is needed since it will check if the required tables and columns exist and will create them when needed.

API

constructor(server, dbConfig)

Initiates the object with the server object and configuration to connect to the database. The database connection looks like this:

"database": {
    "connection": {
        "host": "127.0.0.1",
        "port": 3306,
        "user": "root",
        "password": "root",
        "database": "roadwork-example"
    },
    "client": "mysql",
    "pool": {
        "min": 2,
        "max": 10
    }
}

with the following available clients:

npm install pg --save
npm install mysql --save
npm install mariasql --save
npm install sqlite3 --save

createRequiredTables()

Check if the required tables and columns exist and create them if needed. This returns a promise since it happens async