1.0.4 • Published 6 years ago

logup-client v1.0.4

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
6 years ago

Node.js SDK for LogUp Project

A simple Node.js SDK to integrate with LogUp public API.

Requirement

Getting Started

Install the package directly from npm

npm install logup-client --save

Make sure you have an account on LogUp with a gate to use as test. You can see how to create that on our public documentation.

Initialize the LogUp Client Object

const LogupClient = require("logup-client");

const logupClient = new LogupClient({
    version: "v1_1",  //API version
    gateId: "{you-gate-id}",
    gateSecretKey: "{your-gate-secret-key}"
});

Now you are ready to verify the token received. On the redirect page where the user is sent to after the LogUp, get from the URL those parameters:

  • logupToken = the unique token to verify user identity
  • isNewUser = true if the user is new for you, false if the user said he has already an account. More about this parameter

Verify user identity

This code is how you can verify that the token received is correct and see who the user is.

var logupToken = "{very-long-token-received-as-URL-query-parameter}";
var isNewUser = true || false; //{true-false-boolean};
logupClient.isLogUpVerified(logupToken, isNewUser, function (isSuccess, message) {
    //handle your response
    if (isSuccess) {
        //user was successfully logged in!
        //get the subscription object
        var subscription = logupClient.subscription;
    } else {
        //ops, the user did not log in. See the message
    }
});

Subscription

With the subscription object you can get more information about the actor who logged in, as well as access to its saved DB. A DB is a key-value store of parameters you can associated to your user. Read mode about DB.

Read the values

This is how a Subscription object is made

subscription.idSubscription;  //the id of the subscription
subscription.idActor;  //the id of the actor that access the subscription
subscription.isNewUser;  //true if it is a new user, false otherwise

For difference explanation between id actor and id subscription see our documentation.

Get Subscription DB values

To read DB values of that subscription.

subscription.getDb(function (idSubscription, db) {
        //db is a JSON array
        console.log("idSubscription retrieved: " + idSubscription);
        console.log("With this DB associated: ");
        console.log(db);
    });

Add / Update DB values

Add or update a DB value.

var data = {
    "keyTest": 1,
    "secondKey": "test-value",
    "boolValue": true
};
subscription.updateDbValue(data, function (idSubscription, db) {
                              //the returned DB is the whole DB related to the subscription
                          });

Some limitations may apply to the values you want to add to a DB: read them in our documentation.

When you update or add values, you do not need to give all the previous values, but just those you need to update / add. The db in response will have all the values currently stored in the DB.

Delete DB values

var keys = ["keyTest", "secondKey"];
subscription.deleteDbValue(keys, function (idSubscription, db) {
                              //the returned DB is the whole DB related to the subscription
                          });

Enter an array with one or more strings representing the keys you want to remove from the DB. The response will have the current db without the removed values.

Operate on custom subscription

If you want to operate on a subscription that is not the one retrieved during login, you need to create a new Subscription object with the subscription id you are looking for. This is an example:

var subsIId = "sub_XXXXXXXXXXX";
var subscription = logupClient.getSubscription(subsId);
//on subscription now you can perform all the operation mentioned above

Remember that if you create a custom new subscription, you won't have the values of

  • idActor
  • isNewUser

Since they are related to the user trying to access your website, and not to a generic loaded subscription.

Example: match your user id with subscription id

A good example of how to use our Subscription DB storage is to store the value of your user id that matches our subscription id. This is how we would perform that:

//check if the token is verified
var logupToken = "{very-long-token-received-as-URL-query-parameter}";
var isNewUser = true || false; //{true-false-boolean};
logupClient.isLogUpVerified(logupToken, isNewUser, function (isSuccess, message) {
    //handle your response
    if (isSuccess) {
        //user was successfully logged in!
        //get the subscription object
        var subscription = logupClient.subscription;
        var db = subscription.getDb(function (idSubscription, db) {
                                      if (typeof(db.idUser)==="undefined") {
                                          //no idUser set. It is a new user
                                          saveIdUser(subscription);
                                      } else {
                                          var idUser = db.idUser;  //retrieve the idUser saved
                                      }
                                  });
        
    } else {
        //ops, the user did not log in. See the message
    }
});

/**
* Save in LogUp DB the idUser associated to this subscription
*/
function saveIdUser(subscription) {
    //generate a new idUser for this user
    var idUser = "your-id-user";
    var data = {
        "idUser": idUser
    };
    subscription.updateDbValue(data, function (idSubscription, db) {
                                  //the returned DB is the whole DB related to the subscription
                              });
}