resin-token v4.2.2
resin-token
Resin.io session token utilities.
Role
The intention of this module is to provide low level access to how a Resin.io session token is parsed and persisted.
THIS MODULE IS LOW LEVEL AND IS NOT MEANT TO BE USED BY END USERS DIRECTLY.
Unless you know what you're doing, use the Resin SDK instead.
Installation
Install resin-token
by running:
$ npm install --save resin-token
Documentation
The module returns a factory function that you use to get an instance of the token module.
It accepts the following params:
Param | Type | Description |
---|---|---|
options | Object | options |
options.dataDirectory | string | the directory to use for storage in Node.js. Ignored in the browser. |
Example
const token = require('resin-token')({
dataDirectory: '/opt/cache/resin'
})
- token
- ~parse(token) ⇒ Promise.<Object>
- ~isValid(token) ⇒ Promise.<Boolean>
- ~isExpired() ⇒ Promise.<boolean>
- ~set(token) ⇒ Promise.<String>
- ~get() ⇒ Promise.<String>
- ~has() ⇒ Promise.<Boolean>
- ~remove() ⇒ Promise
- ~getData() ⇒ Promise.<Object>
- ~getProperty(property) ⇒ Promise.<*>
- ~getUsername() ⇒ Promise.<String>
- ~getUserId() ⇒ Promise.<Number>
- ~getEmail() ⇒ Promise.<String>
- ~getAge() ⇒ Promise.<Number>
token~parse(token) ⇒ Promise.<Object>
This function does't save the token. Use token.set()
if you want to persist it afterwards.
The returned promise is rejected if the token is invalid.
Kind: inner method of token
Summary: Parse a token
Returns: Promise.<Object> - parsed token
Access: public
Param | Type | Description |
---|---|---|
token | String | token |
Example
token.parse('...').then((parsedTokenData) => {
console.log(parsedTokenData);
});
token~isValid(token) ⇒ Promise.<Boolean>
Kind: inner method of token
Summary: Check if a token is valid
Returns: Promise.<Boolean> - is valid
Access: public
Param | Type | Description |
---|---|---|
token | String | token |
Example
token.isValid('...').then((isValid) => {
if (isValid) {
console.log('The token is valid!');
}
});
token~isExpired() ⇒ Promise.<boolean>
Kind: inner method of token
Summary: Check if the given token has expired
Access: public
Example
token.isExpired(jwtToken).then((isExpired) => {
console.log(isExpired);
});
token~set(token) ⇒ Promise.<String>
Kind: inner method of token
Summary: Set the token
Returns: Promise.<String> - token
Access: public
Param | Type | Description |
---|---|---|
token | String | token |
Example
token.set('...');
token~get() ⇒ Promise.<String>
This function resolved to undefined if no token.
Kind: inner method of token
Summary: Get the token
Returns: Promise.<String> - token
Access: public
Example
token.get().then((sessionToken) => {
console.log(sessionToken);
});
token~has() ⇒ Promise.<Boolean>
Kind: inner method of token
Summary: Has a token
Returns: Promise.<Boolean> - has token
Access: public
Example
token.has().then((hasToken) => {
if (hasToken) {
console.log('There is a token!');
} else {
console.log('There is not a token!');
}
});
token~remove() ⇒ Promise
This promise is not rejected if there was no token at the time of removal.
Kind: inner method of token
Summary: Remove the token
Access: public
Example
token.remove();
token~getData() ⇒ Promise.<Object>
It will resolve to undefined
if there's no saved token
Kind: inner method of token
Summary: Get the data encoded in the saved token
Returns: Promise.<Object> - token data
Access: public
Example
token.getData().then((parsedTokenData) => {
console.log(parsedTokenData);
});
token~getProperty(property) ⇒ Promise.<*>
This function resolves to undefined
for any property name if there is no token.
It also resolved to undefined
if the property name is invalid.
Kind: inner method of token
Summary: Get a property from a saved token
Returns: Promise.<*> - property value
Access: public
Param | Type | Description |
---|---|---|
property | String | property name |
Example
token.getProperty('username').then((username) => {
console.log(username);
});
token~getUsername() ⇒ Promise.<String>
This function resolves to undefined
if there is no token
Kind: inner method of token
Summary: Get the username of the saved token
Returns: Promise.<String> - username
Access: public
Example
token.getUsername().then((username) => {
console.log(username);
});
token~getUserId() ⇒ Promise.<Number>
This function resolves to undefined
if there is no token
Kind: inner method of token
Summary: Get the user id of the saved token
Returns: Promise.<Number> - user id
Access: public
Example
token.getUserId().then((userId) => {
console.log(userId);
});
token~getEmail() ⇒ Promise.<String>
This function resolves to undefined
if there is no token
Kind: inner method of token
Summary: Get the email of the saved token
Returns: Promise.<String> - email
Access: public
Example
token.getEmail().then((email) =>
console.log(email);
});
token~getAge() ⇒ Promise.<Number>
This function resolves to undefined
if there is no token
Kind: inner method of token
Summary: Get the age of the saved token
Returns: Promise.<Number> - age in milliseconds
Access: public
Example
token.getAge().then((age) => {
console.log(age);
});
Support
If you're having any problem, please raise an issue on GitHub and the Resin.io team will be happy to help.
Tests
Run the test suite by doing:
$ npm test
Contribute
- Issue Tracker: github.com/resin-io/resin-token/issues
- Source Code: github.com/resin-io/resin-token
Before submitting a PR, please make sure that you include tests, and that coffeelint runs without any warning:
$ npm run lint
License
The project is licensed under the Apache 2.0 license.