@dav-nazaryan/brainstorm-task

1.1.0 • Public • Published

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

init(id, [options])Promise

Create new auth instance and save it inside module scope

get(id)Auth

Get instance from instances scope

callInstanceMethod(id, method, data)

Call method of auth instance without getting it

activate(id, userId)

Activate user in mongo for current auth instance

deactivate(id, userId)

Deactivate user in mongo for current auth instance

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

Register new user in mongo for current auth instance

getUser(id, login, password)

Get user object from mongo for current auth instance

logIn(id, login, password)object

Login registered user for current auth instance

bearer(id, token)

Check user access token for current auth instance

refresh(id, accessToken, refreshToken)

Check user access token for current auth instance

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

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

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

Update user login or password

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

Readme

Keywords

none

Package Sidebar

Install

npm i @dav-nazaryan/brainstorm-task

Weekly Downloads

1

Version

1.1.0

License

ISC

Unpacked Size

69.1 kB

Total Files

21

Last publish

Collaborators

  • dav-nazaryan