Skip to content

Commit

Permalink
document singleton behavior, how to create new instances
Browse files Browse the repository at this point in the history
and how to share state between multiple instances.

Fixes #10
  • Loading branch information
markstos committed Apr 2, 2021
1 parent 999216d commit d1a33fb
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ methods below.

### <a id='manager'>`Manager`</a>

To create a new Manager instance use `new auth.Manager();`

For many projects this is not required as the single instance exported by default is
sufficient.

#### <a id='manager.role'>`role(role, getter)`</a>

* **role** `string` - Role name (e.g. 'organization.owner').
Expand Down Expand Up @@ -250,6 +255,47 @@ Thrown on configuration error.
Passed down the middleware chain when a user is not authorized to perform an
action.

## Using `authorized` in large projects

The `authorized` module exports a singleton object. For large projects, you may want to
to create multiple instances of `authorized` and possibly share code between them.

For a complete separate instance use `new auth.Manager();`.

You may also want to add some default entities, roles and actions of your own which then share
with independent instances. Here's a pattern you can use to solve that.

Create a sub-class which calls an `initialize()` method in the constructorl
and then add custom additions there:

```javascript
var Authorized = require('authorized')

// Extend authorized with some roles, entities and actions that are always available.
class CorpAuthorized extends Authorized.Manager {
constructor(options) {
super(options);
this.initialize();
}

initialize() {
const auth = this;
auth.entity('admin', (req,done)=>{
// Custom logic goes here
return done();
}
})
module.exports = CorpAuthorized;

// ...

const Manager = require('./path/to/your/subclass');

// Each instance now has a unique instance with your default rules.
const auth = new Authorized.Manager();

```
## What else?
This package is strictly about authorization. For a full-featured
Expand Down

0 comments on commit d1a33fb

Please sign in to comment.