1.0.5 • Published 6 years ago
iox-auth v1.0.5
IOX-AUTH
Requirements
For password hash.
Compatible
Install
npm install iox-auth --save
Notice
- Before starting this framework you need to configure iox-stateless
- for ease of documentation ioxAuth is equivalent to require ('iox-auth')
const ioxAuth = require('iox-auth')
Select password hash
- Install the desired dependency.
- Start with the code below the selected dependency.
- Set before starting any iox-auth function
- bcrypt:
ioxAuth.Hash = require('bcrypt')
- bcryptjs:
ioxAuth.Hash = require('bcryptjs')
Adapters
Supports the following frameworks to fetch the user from the database.
- Knex
/**
* Data from knex
*
* @param knex knex instance
* @param table table name
* @param email name of email field
* @param password name of password field
* @param id name of id field
* @param group name of group field
*/
ioxAuth.Knex(knex, table, email = 'email', password = 'password', id = 'id', group = 'group')
- Objection
/**
* Data from objection.js
*
* @param Model Model
* @param email name of email field
* @param password name of password field
* @param id name of id field
* @param group name of group field
*/
ioxAuth.Objection(Model, email = 'email', password = 'password', id = 'id', group = 'group')
Middleware
To install middleware:
const Koa = require('koa')
const app = new Koa()
router.use(ioxAuth.Middleware)
....
This framework adds in the context of koa the 'auth' object and has the following functions:
/**
* Check user is authenticated
*
* if isAuth is true you must be authenticated
* if isAuth is false cannot be authenticated
*
* @param isAuth (Boolean) // default true
* @return boolean isAuth ? authenticated : not authenticated
*/
- ctx.auth.authenticated(isAuth: Boolean)
/**
* Checks if user is from group
*
* @param name (String) The name of group
* @return boolean If user is from group
*/
ctx.auth.group(name)
/**
- New in version 1.0.3
- Checks if the user is in any Checks if the user is in any of the groupsps.
- @param name (String[]) GroupsChecks if the user is in any of the groups
- @return boolean If the user is in any of the groups
*/
- ctx.auth.isGroup(groups)
Provider
The providers are data entry and error output.
/**
* Provider for restful.
* Receive by the bodyparser the fields:
* - email
* - password
*/
ioxAuth.Rest()
Router
/**
* Route to Generate Credentials
*
* @param provider Input Provider (Rest)
* @param source Data source (Knex/Objection)
* @return function(ctx) ...
*/
ioxAuth.Router(provider, source))
const Koa = require('koa')
const app = new Koa()
...
router.post('/api/auth', ioxAuth.Router(ioxAuth.Rest(), your adapter))
....
Versions
- 1.0.5
Added hash selection, use bcrypt or bcryptjs
- 1.0.4
The bcrypt dependency has been moved to "devDependencies"
- 1.0.3
Checks if the user is in any of the groups