4.2.2 • Published 5 years ago

resin-token v4.2.2

Weekly downloads
136
License
Apache-2.0
Repository
github
Last release
5 years ago

resin-token

npm version dependencies Build Status Build status

Join our online chat at Gitter chat

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:

ParamTypeDescription
optionsObjectoptions
options.dataDirectorystringthe 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>

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

ParamTypeDescription
tokenStringtoken

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

ParamTypeDescription
tokenStringtoken

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

ParamTypeDescription
tokenStringtoken

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

ParamTypeDescription
propertyStringproperty 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

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.

4.2.2

5 years ago

4.2.1

7 years ago

4.2.0

7 years ago

4.1.0

7 years ago

4.0.0

7 years ago

3.0.0

7 years ago

2.4.3

8 years ago

2.4.2

9 years ago

2.4.1

9 years ago

2.4.0

9 years ago

2.3.0

9 years ago

2.2.0

9 years ago

2.1.0

9 years ago

2.0.0

9 years ago

1.3.0

9 years ago

1.2.0

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago