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

universal-auth

v0.2.0

Published

Universal Auth to be used when rendering on the server or client.

Downloads

5

Readme

Universal Auth

Universal Auth to be used when rendering on the server or client.

Goal

To provide a simple, consistent, and dynamic auth library that can be used by any front-end framework to render on the server or client.

Installation

$ npm install --save universal-auth

Example

var Auth = require('universal-auth');

// Create new auth object
var auth = Auth();

// User to be used for the session.
var user = {
    name: 'John Doe',
    scope: ['user', 'user-123']
};

// Sets the user as the session.
auth.session.set(user);

// Gets the users session.
auth.sesson.get(); // { name: 'John Doe', scope: ['user', 'user-123'] }

// Checks if the session has been set.
auth.isAuthenticated(); // true

// Checks if the session has the scope user.
auth.isAuthorized('user'); // true

// Checks if the session has the scope admin.
auth.isAuthorized('admin'); // false

// Checks if the session has the scope admin or user. Typically used for role based authorization.
auth.isAuthorized(['admin', 'user']); // true

// This is typically provided by a router location.
var routerLocation = {
    params: {
        id: 123
    }
};
/**
 * Dynamic check with the router to see if the session scope has admin
 * or
 * based on the router params id if the session scope has 'user-123'.
 */  
auth.isAuthorized(['admin', 'user-{params.id}'], routerLocation); // true

// Clears the session
auth.session.clear();

API

Auth()

Creates a new auth object that is used for the user and their session.

var auth = Auth();

auth.session.set(session)

Sets the users session in the auth object. If in a browser it will also set the session in localStorage so the session is retained when your app is reloaded by the browser.

To use auth.session.isAuthorized you will need to have a scope property on the session object.

auth.session.get()

Returns the current user session from the auth object or null. In the browser if the session is not found it will check localStorage.

auth.session.clear()

Clears the user session from the auth object. If in the browser it will also clear the session from localStorage.

auth.isAuthenticated()

Returns true or false based on if the user session has been set. If in the browser it will also check localStorage.

auth.isAuthorized(scope, [templateObj])

Returns true or false based on if the users session scope matches the passed in scope. This is great for role based authorization.

If no scope, null, or false is passed in then an automatic true will be returned.

// User to be used for the session
var user = {
    name: 'John Doe',
    scope: 'user'
};

auth.isAuthorized(); // true
auth.isAuthorized(false); // true
auth.isAuthorized(true); // false

// Sets the user as the session.
auth.session.set(user);
auth.isAuthorized(true); // true
auth.isAuthorized('user'); // true
auth.isAuthorized('admin'); // false
auth.isAuthorized(['admin', 'user']); // true

You are also able to have dynamic authorization based on an object you pass as a second parameter. It will be used to fill in the scope template.

This is great for router based auth. Maybe you only want an item available if its the actual users profile and that is known based on the router params.

// User to be used for the session
var user = {
    name: 'John Doe',
    scope: ['user', 'user-123']
};

// Sets the user as the session.
auth.session.set(user);

auth.isAuthorized('user'); // true
auth.isAuthorized('admin'); // false
auth.isAuthorized('user-123'); // true
auth.isAuthorized(['admin', 'user']); // true
auth.isAuthorized('user-{params.id}', { params: { id: 123 } }); // true
auth.isAuthorized('user-{params.id}', { params: { id: 234 } }); // false
auth.isAuthorized(['admin', 'user-{params.id}'], { params: { id: 123 } }); // true
auth.isAuthorized(['admin', 'user-{query.users.0}'], { query: { users: ['123', '456', '789'] } }); // true
auth.isAuthorized(['admin', 'user-{query.users.-1}'], { query: { users: ['456', '789', '123'] } }); // true
auth.isAuthorized(['admin', 'user-{query.users.-2}'], { query: { users: ['789', '123', '456'] } }); // true
auth.isAuthorized(['admin', 'user-{query.users.-1}'], { query: { users: ['123', '456', '789'] } }); // false