2.2.0 • Published 5 years ago
@cesarbr/knot-cloud-sdk-js-authenticator v2.2.0
knot-cloud-sdk-js-authenticator
KNoT Cloud authenticator service JavaScript library.
Quickstart
Install
npm install --save @cesarbr/knot-cloud-sdk-js-authenticatorRun
KNoTCloudAuthenticator connects to <protocol>://<hostname>:<port> using email and password as credentials. Replace this address with your authenticator instance and the credentials with valid ones.
const KNoTCloudAuthenticator = require("@cesarbr/knot-cloud-sdk-js-authenticator");
const client = new KNoTCloudAuthenticator({
protocol: "https",
hostname: "api.knot.cloud",
port: 443,
});
async function main() {
try {
const user = await client.createUser(
"awesome@email.com",
"strong-password"
);
console.log(user);
const token = client.createToken("awesome@email.com", "strong-password");
console.log(token);
} catch (err) {
console.error(err);
}
}
main();Methods
Constructor(options)
Create a client object that will connect to a KNoT Cloud protocol authenticator instance.
Arguments
configObject JSON object with request details.protocolString (Optional) Either'http'or'https'. Default:'https'.hostnameString KNoT Cloud authenticator instance hostname. Default'api.knot.cloud'.portNumber (Optional) KNoT Cloud authenticator instance port. Default: 80/443.
Example
const KNoTCloudAuthenticator = require("@cesarbr/knot-cloud-sdk-js-authenticator");
const client = new KNoTCloudAuthenticator({
protocol: "https",
hostname: "api.knot.cloud",
port: 443,
});createUser(email, password) <Object>
Creates a new user.
Arguments
emailString User email.passwordString User password in plain text.
Result
userObject JSON object containing the created user's credentials.
Example
const KNoTCloudAuthenticator = require("@cesarbr/knot-cloud-sdk-js-authenticator");
const client = new KNoTCloudAuthenticator({
protocol: "https",
hostname: "api.knot.cloud",
port: 443,
});
async function main() {
try {
const user = await client.createUser(
"awesome@email.com",
"strong-password"
);
} catch (err) {
console.error(err);
}
}
main();
// {
// email: 'awesome@email.com',
// password: 'strong-password',
// }createToken(email, password): <Object>
Generates a new access token for the user.
Arguments
emailString User email.passwordString User password, in plain text.
Result
tokenObject JSON object containing the user's new access token.
Example
const KNoTCloudAuthenticator = require("@cesarbr/knot-cloud-sdk-js-authenticator");
const client = new KNoTCloudAuthenticator({
protocol: "https",
hostname: "api.knot.cloud",
port: 443,
});
async function main() {
try {
const token = await client.createToken(
"awesome@email.com",
"strong-password"
);
console.log(token);
} catch (err) {
console.error(err);
}
}
main();
// {
// token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODcxMjkzNzIsImlhdCI6MTU4NzA5MzM3MiwiaXNzIjoibWFpbmZsdXguYXV0aG4iLCJzdWIiOiJkYWRhdmR2YkBrbm90LmNvbSIsInR5cGUiOjB9._lbRa2fzI_CvEorbEACVAf2UnHvkiCOORY55wCWUGAs'
// }createSession(token): <Object>
Generates a new session ID for the user, which will be used to receive thing's data.
Arguments
tokenString User or application token.
Result
sessionObject JSON object containing the user's new session ID.idString user session ID.
Example
const KNoTCloudAuthenticator = require("@cesarbr/knot-cloud-sdk-js-authenticator");
const client = new KNoTCloudAuthenticator({
protocol: "https",
hostname: "api.knot.cloud",
port: 443,
});
async function main() {
try {
const { token } = await client.createToken(
"awesome@email.com",
"strong-password"
);
const session = await client.createSession(token);
console.log(session);
} catch (err) {
console.error(err);
}
}
main();
// {
// "id": "1t4izjcdHSYxfEZ9DUWMNewtlpR"
// }