Skip to content

Commit

Permalink
feat: deprecate tokenable as it is not necessary on the objection con…
Browse files Browse the repository at this point in the history
…text
  • Loading branch information
calvinl committed May 20, 2018
1 parent d472ceb commit 6172a01
Show file tree
Hide file tree
Showing 7 changed files with 913 additions and 1,111 deletions.
53 changes: 2 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ This package includes plugins useful for authentication for websites:

- **Authenticatable** - Generates hashed passwords for a user model. Uses `bcrypt` under the hood.
- **Recoverable** - Generates password reset tokens.
- **Tokenable** - Generates JSON Web Tokens for API authentication. Uses `jsonwebtoken` under the hood.

## Installation
```
Expand Down Expand Up @@ -96,60 +95,13 @@ The field that the expiration date is stored on.
#### `expiresIn` (defaults to `60` minutes)
The expiration time of the token, in minutes.


### Tokenable

```js
// Import the plugin.
const { Tokenable } = require('objection-auth');
const { Model } = require('objection');

// Mixin the plugin.
const TokenableModel = Tokenable({
// expiration time in minutes (default: 7 days)
expiresIn: 10080,
// the secret token to use to authenticate the JWT
secretToken: '!secret!'
})(Model);

// Create your model.
class User extends TokenableModel {
// ...code
}
```

#### Generate a JWT

```js
const user = await User.query().where('id', 1);

await user.generateJWT();
```

#### Decode a JWT

```js
const user = await User.query().where('id', 1);

await user.decodeJWT();
```

#### Options
#### `expiresIn` (defaults to `10080` (7 days))
The expiration time in minutes.

#### `secretOrPrivateKey`
A string, buffer, or object containing either the secret for HMAC algorithms or
the PEM encoded private key for RSA and ECDSA. See the full options
documentation for [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback).

## Chaining Plugins

These plugins can be used together by composing the plugins together:

```js

const { Authenticatable, Recoverable, Tokenable } = require('objection-auth');
const { Authenticatable, Recoverable } = require('objection-auth');
const { compose, Model } = require('objection');

const mixins = compose(
Expand All @@ -158,8 +110,7 @@ const mixins = compose(
tokenField: 'resetPasswordToken',
tokenExpField: 'resetPasswordExp',
expiresIn: 60
}),
Tokenable({ secretOrPrivateKey: 'secret' })
})
);

class User extends mixins(Model) {
Expand Down
Loading

0 comments on commit 6172a01

Please sign in to comment.