# logup-client

> Node.js SDK for LogUp public API

Latest version **1.0.4** (published 2018-02-28) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install logup-client
pnpm add logup-client
yarn add logup-client
bun add logup-client
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2018-02-28 |
| First published | 2018-02-28 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 120.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Antonio Seveso |
| Maintainers | 70nyit |
| Keywords | logup, security, passwordless, logup, client, logup-client, logup, node |

## Links

- npm: https://www.npmjs.com/package/logup-client
- Repository: https://github.com/logupinc/logup-sdk-node
- Homepage: https://github.com/logupinc/logup-sdk-node#readme
- Issues: https://github.com/logupinc/logup-sdk-node/issues
- npm.io page: https://npm.io/package/logup-client

## Dependencies (1)

- [node-rest-client](https://npm.io/package/node-rest-client.md) ^3.1.0

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2018-02-28
- 1.0.3 — 2018-02-28

## README

# Node.js SDK for LogUp Project

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

## Requirement

* [node 5.x](http://nodejs.org/download/) or higher
* [LogUp Developer account](https://www.logup.co)

## Getting Started

Install the package directly from npm

    npm install logup-client --save

Make sure you have an [account on LogUp](https://www.logup.co) with a gate to use as test. You can see how to create that on our [public documentation](https://docs.logup.co).

### Initialize the LogUp Client Object

```javascript
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](https://docs.logup.co/access.html#response-parameters)

### Verify user identity

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

```javascript
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](https://docs.logup.co/subscription.html).

### Read the values

This is how a Subscription object is made
```javascript
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](https://docs.logup.co/access.html#subscription-and-actor-difference).

### Get Subscription DB values
To read DB values of that subscription.
```javascript
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.
```javascript
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](https://docs.logup.co/subscription.html).

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
```javascript
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:

```javascript
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:

```javascript
//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
                              });
}
```

---
_Source: https://npm.io/package/logup-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
