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

@dav-nazaryan/brainstorm-task

v1.1.0

Published

JWT Bearer Auth module

Downloads

13

Readme

Usage

Init new instance

const auth = require('@dav-nazaryan/brainstorm-task');

auth.init('admin', {
  mongoUrl: 'mongodb://localhost:27017/auth-db',
}).then((authInstance) => {
  // use auth instance methods
});

Usage example along with express and mongoose

const auth = require('@dav-nazaryan/brainstorm-task')

const mongoose = require('mongoose');
const express = require('express');
const app = express();

const dependencies = [
  mongoose.connect('mongodb://localhost:27017/auth-demo', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
  }),
  auth.init('user', {
    mongoUrl: 'mongodb://localhost:27017/auth-user',
  }),
];

Promise.all(dependencies).then(() => {
  app.listen(3000, () => {
    console.log('Example app listening on port 3000!');
  });
});

Init instance on start and get it inside other file

server.js

const auth = require('@dav-nazaryan/brainstorm-task');

const express = require('express');
const app = express();

auth.init('user', {
  mongoUrl: 'mongodb://localhost:27017/auth-db',
}).then((authInstance) => {
  app.listen(3000, () => {
    console.log('Example app listening on port 3000!');
  });
});

user.router.js

const express = require('express');
const router = express.Router();

const auth = require('@dav-nazaryan/brainstorm-task');
const userAuth = auth.get('user');

router.post('/register', async (req, res) => {
  const user = await userAuth.register(req.body.login, req.body.password);
  res.status(201).send(user);
});

Usage without getting an instance

server.js

const auth = require('@dav-nazaryan/brainstorm-task')

const mongoose = require('mongoose');
const express = require('express');
const app = express();

const dependencies = [
  mongoose.connect('mongodb://localhost:27017/auth-demo', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
  }),
  // you can init multiple instances
  auth.init('user', {
    mongoUrl: 'mongodb://localhost:27017/auth-admin',
  }),
  auth.init('user', {
      mongoUrl: 'mongodb://localhost:27017/auth-user',
    }),
];

Promise.all(dependencies).then(() => {
  app.listen(3000, () => {
    console.log('Example app listening on port 3000!');
  });
});

admin.router.js

const express = require('express');
const router = express.Router();

const auth = require('@dav-nazaryan/brainstorm-task');

router.post('/update', async (req, res, next) => {
  try {
    auth.bearer('admin', req.headers.authorization);
    next();
  } catch (e) {
    next(e);
  }
});

API

Main module

Auth

Kind: global constant

init(id, [options]) ⇒ Promise

Create new auth instance and save it inside module scope

Kind: global function

| Param | Type | Description | | --- | --- | --- | | id | string | unique id of auth instance | | [options] | object | new instance configs |

get(id) ⇒ Auth

Get instance from instances scope

Kind: global function Returns: Auth - - auth object instance

| Param | Type | Description | | --- | --- | --- | | id | string | unique id of auth instance |

callInstanceMethod(id, method, data)

Call method of auth instance without getting it

Kind: global function

| Param | Type | Description | | --- | --- | --- | | id | string | instance id | | method | string | method to call | | data | object | data that need to be provided ot method |

activate(id, userId)

Activate user in mongo for current auth instance

Kind: global function

| Param | Type | | --- | --- | | id | string | | userId | string |

deactivate(id, userId)

Deactivate user in mongo for current auth instance

Kind: global function

| Param | Type | | --- | --- | | id | string | | userId | string |

register(id, login, password, [options])

Register new user in mongo for current auth instance

Kind: global function

| Param | Type | Default | Description | | --- | --- | --- | --- | | id | string | | instance id | | login | string | | | | password | string | | | | [options] | object | | options object | | [options.login] | boolean | false | log in newly created user and return his token pair, user must be activate for this option | | [options.active] | boolean | | activate newly created user | | [options.getUser] | boolean | false | return new user object, options.login must be false |

getUser(id, login, password)

Get user object from mongo for current auth instance

Kind: global function

| Param | Type | Description | | --- | --- | --- | | id | string | instance id | | login | string | | | password | string | |

logIn(id, login, password) ⇒ object

Login registered user for current auth instance

Kind: global function Returns: object - - access and refresh tokens pair

| Param | Type | Description | | --- | --- | --- | | id | string | instance id | | login | string | | | password | string | |

bearer(id, token)

Check user access token for current auth instance

Kind: global function

| Param | Type | Description | | --- | --- | --- | | id | string | instance id | | token | string | jwt access token |

refresh(id, accessToken, refreshToken)

Check user access token for current auth instance

Kind: global function

| Param | Type | Description | | --- | --- | --- | | id | string | instance id | | accessToken | string | jwt access token | | refreshToken | string | one time refresh token |

logOut(id, userId, [options], login, password)

Cut the users sessions via removing token/all tokens for current auth instance

Kind: global function

| Param | Type | Default | Description | | --- | --- | --- | --- | | id | string | | instance id | | userId | string | | user mongo objectId | | [options] | object | | options object | | [options.authByCredentials] | boolean | false | use user_id or get it via checking credentials | | [options.hard] | boolean | false | cut all user sessions (logout from all devices) | | [options.refreshToken] | boolean | false | use user_id or get it via checking credentials | | login | string | | | | password | string | | |

update(id, userId, [login], [password])

Update user login or password

Kind: global function

| Param | Type | Description | | --- | --- | --- | | id | string | instance id | | userId | string | user mongo objectId | | [login] | string | | | [password] | string | |

Auth Instance

Kind: global class

new Auth([options])

Returns: auth - - new auth object

| Param | Type | Description | | --- | --- | --- | | [options] | object | options object | | [options.mongoUrl] | object | mongo connection url | | [options.collections.user] | object | user collection name | | [options.collections.token] | object | token collection name |

auth.connect(enforcer)

Connect Auth object to mongo

Kind: instance method of Auth

| Param | Type | Description | | --- | --- | --- | | enforcer | symbol | to avoid this method calling out of module |

auth.initModels(enforcer)

Add models to instance, it can be done after instance db connect

Kind: instance method of Auth

| Param | Type | Description | | --- | --- | --- | | enforcer | symbol | to avoid this method calling out of module |

auth.validateCredentials(login, password)

Validate login and password

Kind: instance method of Auth

| Param | Type | | --- | --- | | login | string | | password | string |

auth.register(login, password, [options])

Register new user in mongo for current auth instance

Kind: instance method of Auth

| Param | Type | Default | Description | | --- | --- | --- | --- | | login | string | | | | password | string | | | | [options] | object | | options object | | [options.login] | object | false | log in newly created user and return his token pair, user must be activate for this option | | [options.active] | object | false | activate newly created user | | [options.getUser] | object | false | return new user object, options.login must be false |

auth.getUser(login, password) ⇒ object

Check user auth credentials without logging him in

Kind: instance method of Auth Returns: object - - user object

| Param | Type | | --- | --- | | login | string | | password | string |

auth.logIn(login, password) ⇒ object

Login registered user

Kind: instance method of Auth Returns: object - - access and refresh tokens pair

| Param | Type | | --- | --- | | login | string | | password | string |

auth.activate(userId)

Activate registered user

Kind: instance method of Auth

| Param | Type | | --- | --- | | userId | string |

auth.deactivate(userId)

Deactivate registered user

Kind: instance method of Auth

| Param | Type | | --- | --- | | userId | string |

auth.bearer(token)

Check user access token

Kind: instance method of Auth

| Param | Type | Description | | --- | --- | --- | | token | string | jwt access token |

auth.refresh(accessToken, refreshToken) ⇒ object

Generate new refresh and access tokens pair and update document

Kind: instance method of Auth Returns: object - - access and refresh tokens pair

| Param | Type | Description | | --- | --- | --- | | accessToken | string | jwt access token | | refreshToken | string | one time refresh token |

auth.logOut(userId, [options], login, password)

The logOut method will mostly be used by admins cut the users session, that's why I provide credentials check as an option

Kind: instance method of Auth

| Param | Type | Default | Description | | --- | --- | --- | --- | | userId | string | | user mongo objectId | | [options] | object | | options object | | [options.authByCredentials] | boolean | false | use user_id or get it via checking credentials | | [options.hard] | boolean | false | cut all user sessions (logout from all devices) | | [options.refreshToken] | boolean | false | use user_id or get it via checking credentials | | login | string | | | | password | string | | |

auth.update(userId, [login], [password])

Update user login or password

Kind: instance method of Auth

| Param | Type | Description | | --- | --- | --- | | userId | string | user mongo objectId | | [login] | string | | | [password] | string | |